Bat script to select a folder and update the path in a bat file

Page 1 of 2 12 LastLast

  1. Posts : 6
    Win10
       #1

    Bat script to select a folder and update the path in a bat file


    How do I combine this scripts? i want the highlighted folder path to been passed in to the script that's been generated (rensa_datafiler.bat)



    Its begins with you select a folder:


    @echo off
    Title Folder Selection
    echo( Select folder . . .
    call:FolderSelection "%SourcePath%", SourcePath, "Select Folder"
    echo( : "%SourcePath%"
    pause
    EXIT
    Rem ---------------------------------------------------------------------------------------------------------
    :FolderSelection <SelectedPath> <folder> <Description>
    SetLocal & set "folder=%~1"
    set "dialog=powershell -sta "Add-Type -AssemblyName System.windows.forms^
    |Out-Null;$f=New-Object System.Windows.Forms.FolderBrowserDialog;$f.SelectedPath='%~1';$f.Description='%~3';^
    $f.ShowNewFolderButton=$false;$f.ShowDialog();$f.SelectedPath""
    for /F "delims=" %%I in ('%dialog%') do set "res=%%I"
    EndLocal & (if "%res%" EQU "" (set "%2=%folder%") else (set "%2=%res%"))
    exit/B 0
    Rem ---------------------------------------------------------------------------------------------------------



    And the other script thats replacing the text:

    @echo off &setlocal
    set "search=SoekVaeg"
    set "replace=test"
    set "textfile=template.bat"
    set "newfile=rensa_datafiler.bat"

    (for /f "delims=" %%i in ('findstr /n "^" "%textfile%"') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:%search%=%replace%!"
    echo(!line!
    endlocal
    ))>"%newfile%"
    type "%newfile%"

    Thanks in advance
      My Computer


  2. Posts : 7,607
    Windows 10 Home 20H2
       #2

    qwerty95 said:
    Its begins with you select a folder:
    Do you mean the following dialog box?



    If so, just copy the relevant code from the following batch script:
    Delete Folder in Windows 10
      My Computer


  3. Posts : 6
    Win10
    Thread Starter
       #3

    OK, great, this is the dialog box i want to bee seen, but the big part is how i save the selected path in to the bat file
      My Computer


  4. Posts : 456
    Windows 10
       #4

    It seems that the selected folder is saved in %2...

    Do you want the user to manually specify the file where the search should be done instead of previously defining it in:
    set "textfile=template.bat"?
      My Computer


  5. Posts : 3,274
    Win10
       #5

    You need to do a Call command on the second batch file using something like this near the end of the first batch file:
    Code:
    eg if the next batch file is in the same folder use something like this:
    
    Call .\ NextBatchFile.bat "%SourceFolder%
    or use its full path
    
    Call C:\FullPath\NextBatchFile.bat
    If "template.bat" and "rensa_datafiler.bat" are located inside the %SourceFolder% as well, then in the set "commands", you can use
    Code:
    set "textfile=%SourcePath%\template.bat"
    set "newfile=%SourcePath%\rensa_datafiler.bat"
    Or perhaps combine both batch scripts into one and just make a call on the next section of the script underneath the main part ? Again, you may need to specify where the set "template.bat" and "rensa_datafiler.bat" are located - this example assumes they are oth inside the %SourceFolder%


    Code:
    @echo off
    Title Folder Selection
    echo( Select folder . . .
    call:FolderSelection "%SourcePath%", SourcePath, "Select Folder"
    echo( : "%SourcePath%"
    pause
    
    GOTO :Sub1
    
    Rem ---------------------------------------------------------------------------------------------------------
    :FolderSelection <SelectedPath> <folder> <Description>
    SetLocal & set "folder=%~1"
    set "dialog=powershell -sta "Add-Type -AssemblyName System.windows.forms^
    |Out-Null;$f=New-Object System.Windows.Forms.FolderBrowserDialog;$f.SelectedPath='%~1';$f.Description='%~3';^
    $f.ShowNewFolderButton=$false;$f.ShowDialog();$f.SelectedPath""
    for /F "delims=" %%I in ('%dialog%') do set "res=%%I"
    EndLocal & (if "%res%" EQU "" (set "%2=%folder%") else (set "%2=%res%"))
    exit/B 0
    Rem ---------------------------------------------------------------------------------------------------------
    
    pause
    
    :Sub1
    Call:NextStep "%SourcePath%"
    
    :NextStep
    @echo off &setlocal
    set "search=SoekVaeg"
    set "replace=test"
    set "textfile=%SourcePath%\template.bat"
    set "newfile=%SourcePath%\rensa_datafiler.bat"
    
    (for /f "delims=" %%i in ('findstr /n "^" "%textfile%"') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:%search%=%replace%!"
    echo(!line!
    endlocal
    ))>"%newfile%"
    type "%newfile%"
    
    pause
    exit
    nb: I don't really know exactly what your template.bat and rensa_datafiler.bat are supposed to be doing, so try the scripts after making proper backups of your master files. Or, (recommended) wait for Ricardo or Denis for some input.
      My Computers


  6. Posts : 6
    Win10
    Thread Starter
       #6

    Great!
    Template.bat should after the folder is selected update the path in the Rensa_datafiler.bat

    the Template.bat looks like this:

    ECHO off
    set SourcePath=SoekVaeg

    forfiles.exe /p %SourcePath% /s /d -365 /c "cmd /c del /q /f @file"

    REM Ta bort foldrar äldre än 365 dagar
    forfiles.exe /p %SourcePath% /D -365 /C "cmd /c IF @isdir == TRUE rd /S /Q @Path"

    REM Ta bort tomma foldrar oavsett ålder
    REM for /f "delims=" %%d in ('dir %SourcePath% /s /b /ad ^| sort /r') do rd /Q "%%d"


    The rensa_datafiler.bat looks like this: (This is where i want the folder path to be saved.
    ECHO off
    set SourcePath=test
    forfiles.exe /p %SourcePath% /s /d -365 /c "cmd /c del /q /f @file"
    REM Ta bort foldrar äldre än 365 dagar
    forfiles.exe /p %SourcePath% /D -365 /C "cmd /c IF @isdir == TRUE rd /S /Q @Path"
    REM Ta bort tomma foldrar oavsett ålder
    REM for /f "delims=" %%d in ('dir %SourcePath% /s /b /ad ^| sort /r') do rd /Q "%%d"

    - - - Updated - - -

    One last finetuning question, i want to integrated it as a Visual Studio MSI installer project to been as one last thing to do when it been installed. It is possible to hide the cmd promt and only show the browse folder prompt, and also remove the "press any key to continue" in the cmd window
      My Computer


  7. Posts : 3,274
    Win10
       #7

    I don't know about the Visual Studio part, but to get rid of the "Press any key to continue" prompts, just remove the pause commands ( I only inserted them when testing ).
      My Computers


  8. Posts : 6
    Win10
    Thread Starter
       #8

    Big thanks! @das10
      My Computer


  9. Posts : 456
    Windows 10
       #9

    You can create a VBScript to run your batch script hidden, another method would be to use the task Schuedler.

    Run a batch script hidden using a VBScript example:

    HideMe.vbs:

    Code:
    CreateObject("Wscript.Shell").Run "cmd /c ""c:\PathToBatchFile\batch.bat""",0,false
    batch.bat:

    Code:
    @echo off
    echo.
    echo Hello I'm a batch file
    timeout /t 2 > nul
    md "New Folder"
    exit
    The batch script would still create a folder called "New Folder" but wouldn't show any message nor a black command prompt would appear.

    Last edited by ricardobohner; 15 May 2021 at 15:52.
      My Computer


  10. Posts : 6
    Win10
    Thread Starter
       #10

    Great @ricardobohner it works great!
    Could i when i pointed out the folder get an ok window with the selected path, just to show the end user?
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 06:18.
Find Us




Windows 10 Forums