Batch, making a .ini file to read from [duplicate] - batch-file

This question already has answers here:
Shortest Windows batch file code to get first line of a file set to a variable?
(6 answers)
Closed 1 year ago.
I'm trying to make a Borderlands 3 .bat file to remove certain files (it's a no-intro script). I do want to store the path the user input so the next time the game updates and re-download these files you can remove them fast without asking for a path later on.
This is the first code I have:
set BL3=C:\Program Files (x86)\Steam\steamapps\common\Borderlands 3
set /p BL3= Where's the Borderlands 3 folder? (Default: C:\Program Files (x86)\Steam\steamapps\common\Borderlands 3) =
The full code is here:
#echo off
title Borderlands 3 No Intro Script
:inicreator
set BL3=C:\Program Files (x86)\Steam\steamapps\common\Borderlands 3
set /p BL3= Where's the Borderlands 3 folder? (Default: C:\Program Files (x86)\Steam\steamapps\common\Borderlands 3) =
set errorlevel=0
choice /C YN /N /M "Remove the loading screen as well? (Y = Yes | N = No)"
echo If you haven't cached shaders the screen will be
echo black for awhile if you delete the loading screen.
if '%errorlevel%'=='1' (set LD=yes)
if '%errorlevel%'=='2' (set LD=no)
echo.
echo Creating .ini to store the path for future use ...
echo %BL3% >"bl3settings.ini"
if exist "%BL3%\OakGame\Content\Movies\2KLOGO.mp4" del "%BL3%\OakGame\Content\Movies\2KLOGO.mp4"
if exist "%BL3%\OakGame\Content\Movies\AMDLOGO.mp4" del "%BL3%\OakGame\Content\Movies\AMDLOGO.mp4"
if exist "%BL3%\OakGame\Content\Movies\GBXLOGO.mp4" del "%BL3%\OakGame\Content\Movies\GBXLOGO.mp4"
if '%LD%'=='yes' (if exist "%BL3%\OakGame\Content\Movies\Loading.mp4" del "%BL3%\OakGame\Content\Movies\Loading.mp4")
if '%LD%'=='yes' (echo All done! Removed 2KLOGO.mp4, AMDLOGO.mp4, GBXLOGO.mp4 and Loading.mp4)
if '%LD%'=='no' (echo All done! Removed 2KLOGO.mp4, AMDLOGO.mp4 and GBXLOGO.mp4)
echo These will come back after Boderlands 3 updates.
timeout /t 2 /nobreak>nul
The goal is to write the current loation to a file, then read it and make a variable that we can use...

Using the idea mentioned in SomethingDark's comment, here's a quick untested rewrite of my undertsanding of your script:
#Echo Off
SetLocal EnableExtensions
Title Borderlands 3 - No Intro Script
CD /D "%~dp0" 2>NUL || Exit /B
:DefaultLocation
Set "BL3="
If Exist "%~dp0bl3settings.ini" Set /P "BL3=" 0<"%~dp0bl3settings.ini" 2>NUL
If Not Defined BL3 Set "BL3=%ProgramFiles(x86)%\Steam\steamapps\common\Borderlands 3"
PushD "%BL3%\OakGame\Content\Movies" 2>NUL
If Not ErrorLevel 1 GoTo Actions
:GetLocation
Set "BL3="
Set /P "BL3=Where is the Borderlands 3 directory location?>"
If Not Defined BL3 GoTo GetLocation
PushD "%BL3:"=%\OakGame\Content\Movies" 2>NUL
If ErrorLevel 1 GoTo GetLocation
:Actions
Echo(
Echo Creating .ini to store the path for future use ...
CD 1>"%~dp0bl3settings.ini"
Set "StandardList="2KLOGO.mp4", "AMDLOGO.mp4", "GBXLOGO.mp4""
Set "ExtendedList=%StandardList%, "Loading.mp4""
"%SystemRoot%\System32\choice.exe /M "Remove the loading screen as well"
If ErrorLevel 2 (Del /A /F %StandardList% 2>NUL
Echo All done. Removed: %StandardList%
GoTo Finish
)
Echo If you have not cached shaders, the screen will be black for a while...
%SystemRoot%\System32\timeout.exe /T 2 /NoBreak 1>NUL
Del /A /F %ExtendedList% 2>NUL
Echo All done. Removed: %ExtendedList%
:Finish
PopD
Echo These files will come back after Borderlands 3 updates.
%SystemRoot%\System32\timeout.exe /T 2 /NoBreak 1>NUL

Related

Can not sort tiff image to specific folder using batch script

I am currently trying to create a batch script so I can sort multiple tiff images in a folder by moving it to other specific folder according to user input. What I achieved so far is:
1. Can loop through all the files in the folder
2. Can open the image viewer to view the tiff file
3. Close off the image viewer program
4. Move the tiff file to specific folder
However, I do not know why my set /p is not registering user's input value, when I tried to echo the input out.
cmd gave me "ECHO is off" message.
Appreciate if anyone could give me a hand to resolve this problem.
Many thanks
G
#ECHO OFF
cd "C:\img\"
FOR /R %%f IN (*.tif) DO (
echo Current file is: %%f
start "" "C:\Program Files (x86)\Common Files\Global 360\Imaging\kodakprv.EXE" %%f
set /p name= Action:
IF "%name%" == "1" GOTO ONE
IF "%name%" == "2" GOTO TWO
IF "%name%" == "3" GOTO THREE
ECHO %name%
echo None of the above, BYE!
GOTO END
:ONE
echo "I pressed 1"
taskkill /IM kodakprv.EXE
move %%f "C:\img\1"
cls
:TWO
echo "I pressed 2"
taskkill /IM kodakprv.EXE
move %%f "C:\img\2"
cls
:THREE
echo "I pressed 3"
taskkill /IM kodakprv.EXE
move %%f "C:\img\3"
cls
)
:END
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
cd "C:\img\"
FOR /R %%f IN (*.tif) DO (
echo Current file is: %%f
start "" "C:\Program Files (x86)\Common Files\Global 360\Imaging\kodakprv.EXE" %%f
SET "name="
set /p name= Action:
for %%v in (1 2 3) do IF "!name!" == "%%v" (
set "validaction=Y"
echo "I pressed %%v"
taskkill /IM kodakprv.EXE
move %%f "C:\img\%%v"
pause
cls
)
if not defined validaction (
ECHO !name!
echo None of the above, BYE!
GOTO END
)
)
:END
A few little problems.
First, you can't use labels within a code block (parenthesised series of commands) and second the old favourite, delayedexpansion - if a variable is changed within a code block then unless delayed expansion is in effect, the original value will be used. If delayed expansion is in effect, then %var% refers to the original value and !var! to the current (run-time) value.
Note also that set /p does not change the value of the variable if user response is simply Enter - -hence the set "name=" above to clear its value. Of course, you can omit that line and the last-entered value of name will be used again if you just press Enter.
There are many articles on SO about delayed expansion - just use the search feature.

Processing file(s) and folders via drag and drop, filtering file types

I'm trying to make a batch script that can take any of the following three inputs:
-drag and drop a folder
-drag and drop a single file
-drag and drop a selection of multiple files
and perform a cmd command (transcoding some video with VLC) on all the files so presented that match a list of file types. I've tried doing this a number of different ways, but each way seems to have a showstopping shortcoming of one kind or another.
I can get the script to check for VLC being installed, determine whether it's been given a file, files, or folder, and process an individual file just fine without filtering extensions; it's processing multiple files, processing a folder, and filtering by extension that are tripping me up:
#echo off
Title PPT Transcoder
Mode con cols=120 lines=16
IF [%~1] == "" GoTo :Error_ranDirectly
IF exist "C:\Program Files\VideoLAN\VLC\vlc.exe" (
CD /D "%~1">nul 2>&1 && Goto :Explorer_Folder || Goto :Files
) else (
Goto :Error_VLCNotInstalled
)
Exit /b
::**********************************************************
:Files <File>
Color 0E
ECHO Transcoding %1 to %1_PPT.mp4...
REM I really want to filter for six filetypes here (.mp4, .avi, .mov., .wmv,
REM .flv, .gif) and not affect any others. Forfiles looks like it could
REM work, but I can't figure out how to get it to work on my arguments
REM rather than the entire folder of files that match the filter.
"C:\Program Files\VideoLAN\VLC\vlc.exe" [a bunch of flags] %1 [a bunch more flags]:file{dst=%1_PPT.mp4,no-overwrite} [even more flags] vlc://quit
IF [%~2] == "" Goto :FilesFinished
shift
GoTo :Files
:FilesFinished
Color 0A
Echo Finished processing.
Timeout /T 10 /NoBreak >nul
Exit /b
::**********************************************************
:Explorer_Folder <Folder>
REM This block of code almost works, but i think it malforms the file names
REM fed to vlc. It runs and thinks it's working, but fails to actually
REM accomplish anything.
setlocal
Color 0C
SET /P AREYOUSURE=Transcode all files in folder %1 (Y/[N])?
IF /I "%AREYOUSURE%" NEQ "Y" GOTO :Abort
Color 0F
for %%x in (%1\*.mp4 %1\*.mov %1\*.avi %1\*.wmv %1\*.flv) do (
echo Transcoding %%x to %%x_PPT.mp4...
""C:\Program Files\VideoLAN\VLC\vlc.exe" [a bunch of flags] %%x [a bunch more flags]:file{dst=%%x_PPT.mp4,no-overwrite} [even more flags] vlc://quit
echo Done.)
Timeout /T 10 /NoBreak >nul
endlocal
Exit /b
::**********************************************************
:Error_RanDirectly
Color 0C & echo(
ECHO Drag and drop video files on the icon to transcode them.
Timeout /T 5 /NoBreak >nul
Exit /b
::**********************************************************
:Error_VLCNotInstalled
Color 0C & echo(
ECHO VLC 64-bit (free download from videolan.org) must be installed.
Timeout /T 10 /NoBreak >nul
Exit /b
::**********************************************************
:Abort
Exit /b
I guess to make this a general question: Is there a generally accepted framework for making a batch script handle drag and drop execution intelligently?
Save this as a cmd file and drop directories and files on it. See how it works.
#setlocal ENABLEEXTENSIONS
#rem #set prompt=$G
#call :ShowTypes %*
#pause
#exit /b
:ShowTypes
#echo Processing:
:_showTypes
#rem If it exists with a slash appended to the end,
#rem it's directory, otherwise a file type.
#if exist "%1\\" (#set _type=directory) else (#call :SetFileType "%1")
#if "%1" neq "" (echo %1 as %_type%) else (exit /b 0)
#shift
#goto :_showTypes
:SetFileType
#rem %~x1 is the extension of the first parameter passed by caller.
#set _type=%~x1
#rem Remove the dot from the extension string.
#set _type=%_type:.=%
#rem Now _type is whatever the extension was.
#exit /b 0
Remove the leading # symbol to see that line in action.

Batch Coding - Choose command not returning the correct answer

I am trying to create a batch file which has two options:
Create a series of folders with custom names (defined by variables inputted by the user)
OR
Create the same as before except also, once completed, automatically
copying all appropriate files to the appropriate folders in the
structure.
The idea is that with this batch file I could keep my film projects (As I am a freelance filmmaker working with my brother) in exactly the same folder structures across all 3 of our computers. AKA a uniform Project folder structure can be implemented with a simple double click and a few variables to fill in, while in the case of option 2 any files found on a specified SD Card are also moved to the appropriate folders within the structure.
The actual xcopy commands and mkdir commands etc. work exactly how they are supposed to.
Since the different computers will be allocating different drive letters depending on what is already attached to the computer before entering the SD Card into the card reader, I will never know what the drive letter of the SD Card will be (from which the footage and images will be loaded onto the computers).
For example:
One computer could have an external hard drive in (of which we have a few) at the time of the SD Card being plugged in, while another may have only the SD Card as an external device.
The code below is the complete .bat file including past code which I had tried but didn't work or needed to be excluded for some reason. I have some lines of code in comments so I know what I had tried in the past, these are obviously not meant to be used anymore.
:Menu
ECHO OFF
CLS
ECHO OFF
TITLE Saborknight Productions - Project Creator
ECHO.
ECHO ...............................................
ECHO PRESS the appropriate number to select your task, or 3 to EXIT.
ECHO ...............................................
ECHO.
ECHO 1 - Create Saborknight Default folder Hierarchy within a project folder
::(Disabled)ECHO 2 - Create Saborknight Default folder Hierarchy AND copy ALL files from SD Card to the appropriate folder
ECHO 3 - EXIT
ECHO.
ECHO WARNING - Option 2 will delete the files from the Recording Device automatically. Secure the device before proceeding!
ECHO.
ECHO.
CHOICE /c 123 /n
IF ERRORLEVEL 3 EXIT
IF ERRORLEVEL 2 GOTO :CREATE
IF ERRORLEVEL 1 GOTO :CREATE
::End of Menu
---------------------------------------------------------------------------------------------
:CREATE
::Creates DIR Folder Structure as per Saborknight Productions Standard
ECHO OFF
CLS
::Variables Descriptor
SET /P project_name=Enter the Project Name: || SET project_name=NothingChosen
If "%project_name%"=="NothingChosen" (
GOTO ERROR_PROJECT
) ELSE (
GOTO CREATOR
)
:ERROR_PROJECT
::If no Project Name is entered
ECHO Please enter the Project Name
GOTO CREATE
:CREATOR
::DIR Creator + Confirmation
::Videos DIR
ECHO OFF
MKDIR D:\"Movie Projects"\"%project_name%"\Footage
::Cut from above MKDIR -> \"Day 1"\%device_name%
::Images DIR
ECHO OFF
MKDIR D:\"Movie Projects"\"%project_name%"\Images\%device_name%
::Audio DIR -> Soundtracks
ECHO OFF
MKDIR D:\"Movie Projects"\"%project_name%"\Audio\Soundtracks
::Audio DIR -> Recordings
ECHO OFF
MKDIR D:\"Movie Projects"\"%project_name%"\Audio\Recordings
::Audio DIR -> Sound FX
ECHO OFF
MKDIR D:\"Movie Projects"\"%project_name%"\Audio\"Sound FX"
::Premier Project DIR
ECHO OFF
MKDIR D:\Dropbox\"Project Files"\"%project_name%"
ECHO The Folder Structure has been created
ECHO.
ECHO.
ECHO What would you like to do next?
Echo.
ECHO 1 - View Created Folder Structure
ECHO 2 - Go to Menu
ECHO 3 - EXIT
::Choice is invisible
CHOICE /c 123 /n
IF ERRORLEVEL 3 EXIT
IF ERRORLEVEL 2 GOTO MENU
IF ERRORLEVEL 1 GOTO VIEW
::End of CREATE
---------------------------------------------------------------------------------------------
:COPY
::Creates DIR Folder Structure as per Saborknight Productions Standard + Copying/Moving Files from a selected SD Card connected to the computer
ECHO OFF
CLS
:COPY_PROJECT
::Project Variable Descriptor
SET /P project_name=Enter the Project Name: || SET project_name=NothingChosen
If "%project_name%"=="NothingChosen" (
GOTO ERROR_COPY_PROJECT
) ELSE (
GOTO COPY_DEVICE_NAME
)
::If True, Process continues
:COPY_DEVICE_NAME
::Device Name Variable Descriptor
ECHO.
SET /P device_name=Enter the Name of the Recording Device: || SET device_name=NothingChosen
If "%device_name%"=="NothingChosen" (GOTO DAMN_DEVICE) ELSE (GOTO DEVICE_DIR)
:DEVICE_DIR
::Device DIR Variable Descriptor
ECHO.
::Selecting the location of the SD Card of the Recording Device + Confirmation
ECHO Copy files from drive: E, F, G, H or I?
SET _drive=G
::SETLOCAL EnableDelayedExpansion
::CHOICE /c fgehi
::IF ERRORLEVEL 5 SET _drive=I
::IF ERRORLEVEL 4 SET _drive=H
::IF ERRORLEVEL 3 SET _drive=G
::IF ERRORLEVEL 2 SET _drive=F
::IF ERRORLEVEL 1 SET _drive=E
ECHO.
ECHO You have selected drive %_drive%:
ECHO.
::DIR Creator + Confirmation -> Only if Device Drive is successfully selected
::Videos DIR
ECHO OFF
MKDIR D:\"Movie Projects"\"%project_name%"\Footage\"Day 1"\"%device_name%"
::Images DIR
ECHO OFF
MKDIR D:\"Movie Projects"\"%project_name%"\Images\"%device_name%"
::Audio DIR -> Soundtracks
ECHO OFF
MKDIR D:\"Movie Projects"\"%project_name%"\Audio\Soundtracks
::Audio DIR -> Recordings
ECHO OFF
MKDIR D:\"Movie Projects"\"%project_name%"\Audio\Recordings
::Audio DIR -> Sound FX
ECHO OFF
MKDIR D:\"Movie Projects"\"%project_name%"\Audio\"Sound FX"
::Premier Project DIR
::ECHO OFF
::(Disabled for Testing)MKDIR D:\Dropbox\"Project Files"\"%project_name%"
ECHO.
CHOICE /c YN /m "Would you like files to be deleted automatically from the device? Press Y=Yes or N=No"
IF ERRORLEVEL 2 GOTO NO_DELETE
IF ERRORLEVEL 1 GOTO DELETE
:NO_DELETE
::No Deletion once Copied from Device
::For Copying Videos
ECHO.
ECHO Copying Videos
XCOPY %_drive%:\DCIM\ "D:\Movie Projects\"%project_name%"\Footage\"Day 1"\"%device_name%"\" *.MOV *.mp4 /V /-Y
::COPY %_drive%:\DCIM\ "D:\Movie Projects\"%project_name%"\Footage\"Day 1"\"%device_name%"\" *.MOV *.mp4
::ROBOCOPY %_drive%:\DCIM "D:\"Movie Projects"\"%project_name%"\Footage\"Day 1"\"%device_name%"\*" *.mov *.mp4 /mir /copy:DATO /np /eta /log+:"Log - Footage CopyOnly.txt" /nosd /nodd
PAUSE
::For Copying Images
ECHO.
ECHO Copying Images
XCOPY %_drive%:\DCIM\ "D:\Movie Projects\"%project_name%"\Images\"%device_name%"\" *.MOV *.mp4 /V /-Y
::COPY %_drive%:\DCIM\*.jpg;*.png;*.cr2 "D:\Movie Projects\"%project_name%"\Images\"%device_name%"\" /V /-Y
::ROBOCOPY %_drive%:\DCIM "D:\"Movie Projects"\"%project_name%"\Footage\"Day 1"\"%device_name%"" *.jpg *.png *.cr2 /copy:DATO /mir /np /eta /log+:"Log - Images Copy Only.txt" /nosd /nodd
PAUSE
GOTO SUCCESS_MSG
:DELETE
::Copying with Automatic Deletion once Copied
::For Copying and Deleting Videos
ECHO Copying and Deleting Videos
MOVE %_drive%:\DCIM\*.mov;*.mp4 D:\"Movie Projects"\"%project_name%"\Footage\"Day 1"\"%device_name%"\ /-Y
::ROBOCOPY %_drive%:\DCIM D:\"Movie Projects"\"%project_name%"\Footage\"Day 1"\"%device_name%" *.mov *.mp4 /mov /copy:DATO /np /eta /log+:"Log - Footage Copy Delete.txt" /nosd /nodd
PAUSE
::For Copying and Deleting Images
ECHO Copying and Deleting Images
MOVE %_drive%:\DCIM\*.jpg;*.png;*.cr2 D:\"Movie Projects"\"%project_name%"\Images\"%device_name%"\ /-Y
::ROBOCOPY %_drive%:\DCIM D:\"Movie Projects"\"%project_name%"\Images\"%device_name%" *.jpg *.png *.cr2 /mov /copy:DATO /np /eta /log+:"Log - Images Copy Delete.txt" /nosd /nodd
PAUSE
GOTO SUCCESS_MSG
:SUCCESS_MSG
::Only if all Copying/Moving and DIR Creation Processes are Completed Successfully
ECHO.
ECHO The Folder Structure has been created and files copied/moved
ECHO.
ECHO.
ECHO What would you like to do next?
Echo.
ECHO 1 - View Created Folder Structure
ECHO 2 - Go to Menu
ECHO 3 - EXIT
::Choice is invisible
CHOICE /c 123 /n
IF ERRORLEVEL 3 EXIT
IF ERRORLEVEL 2 GOTO MENU
IF ERRORLEVEL 1 GOTO :VIEW
:DAMN_DEVICE
ECHO.
ECHO Please enter the Device Name, Re-entering the Project Name
ECHO.
GOTO COPY
:ERROR_COPY_PROJECT
::If no Project Name is entered
ECHO.
ECHO Please enter the Project Name
ECHO.
GOTO COPY_PROJECT
ENDLOCAL
::End Of COPY
---------------------------------------------------------------------------------------------
:VIEW
ECHO OFF
CLS
TREE D:\"Movie Projects"\"%project_name%"
ECHo.
ECHO Press any key to return to Menu
ECHO.
PAUSE
GOTO MENU
::End of VIEW
The Problem
In this part of the code, the CHOOSE command wont choose the correct choice:
#ECHO OFF
CHOICE /c fgehi
IF ERRORLEVEL 5 SET _drive=I
IF ERRORLEVEL 4 SET _drive=H
IF ERRORLEVEL 3 SET _drive=E
IF ERRORLEVEL 2 SET _drive=G
IF ERRORLEVEL 1 SET _drive=F
Pause
ECHO.
ECHO You have selected drive %_drive%:
ECHO.
Pause
The Evidence of the Problem being a problem
I tested the Batch file on two separate machines (One by remote access so that the two screens lay side by side) and can be viewed here: Image of the Problem being a problem...
Apologies for the dirtiest code in history with a complete overuse of comments. This was supposed to be a one night project which has turned into a month long project and I still haven't solved it!
Even a programming friend of mine cannot explain why it wont work.
All help much appreciated in advance!!!
Also any alternative suggestions to complete the described tasks above are definitely welcome, I love learning new code!
Saborknight
Your program fail because IF ERRORLEVEL n ... "execute the command if the errorlevel is GREATER OR EQUAL than the number given", that is, you placed the IF commands in the opposite order. This works correctly:
#ECHO OFF
CHOICE /c fgehi
IF ERRORLEVEL 1 SET _drive=F
IF ERRORLEVEL 2 SET _drive=G
IF ERRORLEVEL 3 SET _drive=E
IF ERRORLEVEL 4 SET _drive=H
IF ERRORLEVEL 5 SET _drive=I
Pause
ECHO.
ECHO You have selected drive %_drive%:
ECHO.
Pause
May I suggest you a different, simpler method?
#ECHO OFF
SET option=0FGEHI
CHOICE /c fgehi
CALL SET _drive=%%option:~%ERRORLEVEL%,1%%
ECHO.
ECHO You have selected drive %_drive%:
ECHO.
Pause
As documented here: http://www.robvanderwoude.com/choice.php
By the way, in Windows NT 4 this won't work, since the SET command itself will set an errorlevel (usually 0)!
However, Windows NT makes it easy by storing the latest errorlevel in the environment variable ERRORLEVEL
This works for me:
#ECHO OFF
CHOICE /c fgehi
IF %ERRORLEVEL% EQU 5 SET _drive=I
IF %ERRORLEVEL% EQU 4 SET _drive=H
IF %ERRORLEVEL% EQU 3 SET _drive=E
IF %ERRORLEVEL% EQU 2 SET _drive=G
IF %ERRORLEVEL% EQU 1 SET _drive=F
Pause
ECHO.
ECHO You have selected drive %_drive%:
ECHO.
Pause
Unless you know the proper ritual to ask the Microsoft gods for forgiveness, I would recommend you stay away from batch, it is a lot of trouble.
Even PowerShell is much less of a hassle.

DIR /p pausing not working...?

I am having a problem. I need my code to list all the text files in a big directory and pause at each full screen. I set it up so that users will be able to input the number that is listed beside every txt file in the list but the /p parameter refuses to pause the list...
DIR SAVES\*.txt /B /P |FIND /V /N "//"
ECHO.
ECHO (Press "Q" to EXIT to the main menu)
ECHO.
CHOICE /C DLVQ /N /M "Would you like to DELETE, LOAD, or VIEW a file [D/L/V]?"
IF ERRORLEVEL 4 GOTO MENU
IF ERRORLEVEL 3 GOTO VIEW
IF ERRORLEVEL 2 GOTO LOAD1
IF ERRORLEVEL 1 GOTO DELETE
:DELETE
ECHO.
SET /P C=Choose the file number [#] to DELETE (and press ENTER):
FOR /F "usebackq tokens=1,2* delims=[]" %%d IN (`DIR SAVES\*.txt /B ^|FIND /V /N "//"`) DO (
IF /I !C! EQU q (GOTO MENU)
IF !C! EQU %%d (
ECHO.
CHOICE /N /M "Are you sure you want to DELETE %%e [Y/N]?"
IF ERRORLEVEL 2 GOTO LOAD
DEL /Q "SAVES\%%e"
DEL /Q "SAVES\"%%~ne"_stats"
IF EXIST "SAVES\%%e" (
IF EXIST "SAVES\"%%~ne"_stats" (
GOTO DELETE1
The folder could potentially have hundreds of txt save files and i need the ability to run this script in its own cmd window and pause as the screen fills up with info. Running my whole utility as a .bat means there is NO scrollbar available for users to view all the files. How can I make the /P pause parameter work?
You're piping the output of dir to find. find itself doesn't do pagination and simply outputs anything it matches in the input. dir itself can't do the pagination in this case, because find would be unable to do the input to advance to the next page, so you've effectively disabled pagination.
Try
dir | find | more

Delete .txt files in subfolders using batch script [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Delete files in subfolder using batch script
I have to delete .txt files from a sub folder (with same name). My filepath is like as follows.
d:\test\test1\archive*.txt
d:\test\try\archive*.txt
d:\test\model\archive*.txt
I tried "del" command to delete the ".txt" files in above paths. But there are more than 100 folders in the folder "test". So it is very difficult to use "del" for each and every path.
Except the parent folder name of "archive" folder, everything remains the same for all the paths. So I guess there might be some easy way to delete the files using batch script.
Can anyone guide me whether there is any easy way to delete .txt files using batch script Or I have to repeat "del" for all 100 folders?
del /s *.txt
hope it helps.All the best mate
del /s *.txt will delete all TXT files in all subfolders of current working directory.
(But use that command carefully - wrong parent directory and you are throwing away all textfiles on your computer :) )
Edited
del /s d:\test\archive\*.txt
This should get you all of your text files
Alternatively,
I modified a script I already wrote to look for certain files to move them, this one should go and find files and delete them. It allows you to just choose to which folder by a selection screen.
Please test this on your system before using it though.
#echo off
Title DeleteFilesInSubfolderList
color 0A
SETLOCAL ENABLEDELAYEDEXPANSION
REM ---------------------------
REM *** EDIT VARIABLES BELOW ***
REM ---------------------------
set targetFolder=
REM targetFolder is the location you want to delete from
REM ---------------------------
REM *** DO NOT EDIT BELOW ***
REM ---------------------------
IF NOT DEFINED targetFolder echo.Please type in the full BASE Symform Offline Folder (I.E. U:\targetFolder)
IF NOT DEFINED targetFolder set /p targetFolder=:
cls
echo.Listing folders for: %targetFolder%\^*
echo.-------------------------------
set Index=1
for /d %%D in (%targetFolder%\*) do (
set "Subfolders[!Index!]=%%D"
set /a Index+=1
)
set /a UBound=Index-1
for /l %%i in (1,1,%UBound%) do echo. %%i. !Subfolders[%%i]!
:choiceloop
echo.-------------------------------
set /p Choice=Search for ERRORS in:
if "%Choice%"=="" goto chioceloop
if %Choice% LSS 1 goto choiceloop
if %Choice% GTR %UBound% goto choiceloop
set Subfolder=!Subfolders[%Choice%]!
goto start
:start
TITLE Delete Text Files - %Subfolder%
IF NOT EXIST %ERRPATH% goto notExist
IF EXIST %ERRPATH% echo.%ERRPATH% Exists - Beginning to test-delete files...
echo.Searching for .txt files...
pushd %ERRPATH%
for /r %%a in (*.txt) do (
echo "%%a" "%Subfolder%\%%~nxa"
)
popd
echo.
echo.
verIFy >nul
echo.Execute^?
choice /C:YNX /N /M "(Y)Yes or (N)No:"
IF '%ERRORLEVEL%'=='1' set question1=Y
IF '%ERRORLEVEL%'=='2' set question1=N
IF /I '%question1%'=='Y' goto execute
IF /I '%question1%'=='N' goto end
:execute
echo.%ERRPATH% Exists - Beginning to delete files...
echo.Searching for .txt files...
pushd %ERRPATH%
for /r %%a in (*.txt) do (
del "%%a" "%Subfolder%\%%~nxa"
)
popd
goto end
:end
echo.
echo.
echo.Finished deleting files from %subfolder%
pause
goto choiceloop
ENDLOCAL
exit
REM Created by Trevor Giannetti
REM An unpublished work
REM (October 2012)
If you change the
set targetFolder=
to the folder you want you won't get prompted for the folder.
*Remember when putting the base path in, the format does not include a '\' on the end.
e.g.
d:\test
c:\temp
Hope this helps

Resources