I'm building a menu with this command CHOICE /c 123456789 /N /M "Type your choise"
This is inside a my menu batch script and it calls Tools1.bat it makes everything inside the Tools.bat run with Sub folder reader
IF %ERRORLEVEL% EQU 1 (
#For /F "EOL=? Delims=" %%G In ('Dir /B /S /A:D') Do #PushD "%%G" && (Call "%~dp0Tools1.bat" >NUL 2>&1 & PopD)
)
Tools.bat
ren "asset_database_?_*_?_Release" Asset_Database_Cleaned_0.txt
findstr /i "icon" Asset_Database_Cleaned_0.txt > Asset_Database_Cleaned_1.txt
findstr /i "[(0-9)]" Asset_Database_Cleaned_0.txt > Asset_Database_Cleaned_2.txt
ren "Asset_Database_Cleaned_0.txt" asset_database_9_99_9_Release
ren "%~dp01_Prior_Version\Asset_Database_Cleaned_2.txt" "Prior Version.txt"
ren "%~dp02_Current_Version\Asset_Database_Cleaned_2.txt" "Current Version.txt"
)
I have 2 folders with one file inside each folder
01_Prior_Version
+ -- asset_database_2_09_7_Release
02_Current_Version
+ -- asset_database_3_29_4_Release
the asset_database has random numbers so I use wildcards for that
asset_databaseRelease or asset_database_?__?_Release
These files get renamed to Asset_Database_Cleaned_0.txt
and then I use findstr /i to extract content that I need
I have tried many different ways to add that top script to my menu batch script
I have tried many different ways to add a Sub folder reader using SET & Source etc
IF %ERRORLEVEL% EQU 1 (
#For %%G In ("%~dp01_Prior_Version") Do Set "source=%%~fG"
#For %%G In ("%~dp02_Prior_Version") Do Set "target=%%~fG"
Set "Source1=1_Prior_Version\Asset_Database_Cleaned_1.txt"
Set "Target1=1_Prior_Version\Prior Version.txt"
findstr /i "icon [(0-9)]" %Source1% > %Target1%
ren "Prior\Asset_Database_Cleaned_0.txt" asset_database_9_99_9_Release
I tried using this script, but couldn't put all of that above together
#echo off
FOR /F %%i in ('dir /b/s/a-d') DO (
if "%%~xi" == "" rename "%%~fi" "%%~ni.txt"
)
This is my menu
:Data Finder
echo.
ECHO ################################################################
ECHO ALL YOUR ORIGINAL FILES WILL BE SAVED
ECHO ################################################################
echo.
ECHO Press 1 - Extract Data
ECHO Press 2 - Run PY
echo.
ECHO Thank You
ECHO For all the support
echo.
CHOICE /c 123456789 /N /M "Type your choise"
IF %ERRORLEVEL% EQU 1 (
#For /F "EOL=? Delims=" %%G In ('Dir /B /S /A:D') Do #PushD "%%G" && (Call "%~dp0Tools1.bat" >NUL 2>&1 & PopD)
goto=:Data Finder
)
IF %ERRORLEVEL% EQU 2 (
Call "%~dp0Tools2.py"
goto=:Data Finder
)
When I add this in, the CMD just closes
Related
In a certain path I have some different kinds of file type. Eg., .txt, .bas, .cls, etc.
I need to delete only the text files in that path except few files.
For eg, if the path has a.txt, b.txt, c.txt, aa.bas, bb.cls, it should delete only a.txt. It should not delete b.txt and c.txt (Also it should not delete the other extension files).
To delete all ?.txt files in the root folder, excluding b.txt and c.txt
#echo off
for %%i in (?.txt) do (
if not "%%~nxi"=="c.txt" if not "%%~nxi"=="b.txt" echo del "%%i"
)
To do this in the root and subdirectories:
#echo off
for /R %%i in (?.txt) do (
if not "%%~nxi"=="c.txt" if not "%%~nxi"=="b.txt" echo del "%%i"
)
If the files are to be all *.txt files and not just single digit as per your example (add /R to recurse:
#echo off
for %%i in (*.txt) do (
if not "%%~nxi"=="c.txt" if not "%%~nxi"=="b.txt" echo del "%%i"
)
Similarly, but using findstr to only exclude:
#echo off
for /f %%i in ('dir /b /a-d ^|findstr /vi "b.txt" ^|findstr /vi "c.txt"') do (
echo del "%%i"
)
and to search only include:
#echo off
for /f %%i in ('dir /b /a-d ^|findstr /i "a.txt"') do (
echo del "%%i"
)
and to include and search subdirectories:
#echo off
for /f %%i in ('dir /b /s /a-d ^|findstr /i "a.txt"') do (
echo del "%%i"
)
On all of the above examples, remove echo to actually perform the delete, echo is used as a safety measure and will only display the del result to console.
Edit
Seeing as you specifically have a list of files (as per one of you comments) to exclude, you can use something like this. You have to create a file called exclusion.txt and add the files to exclude in list form:
b.txt
c.txt
file with space.txt
d.txt
Then create the batch file and add the code below. When ran, it will prompt for the file extention to filter on, where you can type an extension. i.e txt or simply press enter to perform a delete on all files, except the excluded ones. Just to be safe, I added an additional for loop to simply echo the files and prompt you if you are sure you want to delete the files.
#echo off
set cnt=0 & set excl= & set ext=
echo(
if not exist exclusion.txt echo You have not created an "exclusion.txt" file. & echo( & echo You need to create it first, then rerun the script & echo( & pause & goto :eof
echo Ensure you have listed all files to be excluded in "exclusion.txt" file
echo(
set /p "ext=Add File extention to search on (txt, pdf, etc), or press enter for all files: "
if not defined ext goto cont
if not "%ext:~0,1%"=="." set "ext=.%ext%"
set "ext=*%ext%"
:cont
setlocal enabledelayedexpansion
for /f "delims=" %%a in (exclusion.txt) do (
set /a cnt+=1
set "nlr!cnt!=%%a"
)
for /l %%i in (1,1,%cnt%) do (
if not defined excl (
set "excl=!nlr%%i!"
) else (
set "excl=!excl! !nlr%%i!"
)
)
echo(
echo WARNING: You are about to delete the following files!!
echo(
for /f "delims=" %%i in ('dir /b /a-d %ext% ^|findstr /VIE "%excl%"') do (
if /i not "%%i"=="exclusion.txt" if not "%%i"=="%~0" echo %%i
)
echo(
Choice /c YN /m "Are you sure you want to delete these files?"
if %errorlevel% equ 2 goto :eof
for /f "delims=" %%i in ('dir /b /a-d %ext% ^|findstr /VIE "%excl%"') do (
if /i not "%%i"=="exclusion.txt" if not "%%i"=="%~0" del %%i
)
I'm using the below. I've used FC and COMP to look at files generated by DIR. No not real sure how to approach this issue.
Basically I need to pause the script, another program will create a new folder, once the user presses enter, it should show the name of the new folder.
(for /d %%i in ("%~dp0") do (
pause>nul|set/p =Add folder..
if exist "%~dp0%%~nxi" echo(%%~i)
)
cmd /k
To list the number of directories in the current path:
This should point you in the right direction, you can run it as is for now to see.
#echo off
:start
echo Press Enter when you want to see the new Foldername:
pause >nul
cls
for /f %%i in ('dir /b /ad /o-d') do set "newfolder=%%i" & goto reveal
:reveal
echo New Folder is "%newfolder%"
goto start
All this does is to sort by the latest modified date of all folders and only echo the latest. Simple as that.
You can also add a counter that will show you the number of folders:
#echo off
:start
echo Press Enter when you want to see the new Foldername:
pause >nul
cls
for /f %%a in ('dir ^| findstr /i "Dir(s)"') do set count=%%a
for /f %%i in ('dir /b /ad /o-d') do set "newfolder=%%i" & goto reveal
:reveal
echo Number of folders: %count%
echo Latest Folder: "%newfolder%"
goto start
Or if you want to see the Full path of the folder, not just the new name of it, use %%~fi:
#echo off
:start
echo Press Enter when you want to see the new Foldername:
pause >nul
cls
for /f %%a in ('dir ^| findstr /i "Dir(s)"') do set count=%%a
for /f %%i in ('dir /b /ad /o-d') do set "newfolder=%%~fi" & goto reveal
:reveal
echo Number of folders: %count%
echo Latest Folder: "%newfolder%"
goto start
And here is a version that does not require any user input, it will detect the new folder for you.
#echo off
set newfold=
set oldfold=
:start
for /f %%i in ('dir /b /ad /o-d') do set "newfold=%%i" & goto reveal
:reveal
if not defined oldfold set "oldfold=%newfold%"
if not "%newfold%"=="%oldfold%" (
echo New Folder detected: %newfold%
set oldfold=%newfold%
)
timeout /t 3 /nobreak >nul
goto start
I'm currently working on some batch file that will be deleting all files from selected usb drive. The code works, but i wanted to add second choice to be sure if user is certain that he choose correct drive or so and the second choice is not responding. No matter which option i'll choose it'll somehow delete all files from this point.
I'm new to batch and to programming also
Here's a code:
#echo off
choice /c YN /t 15 /d n /m "Do you want to delete all files from USB drive? Y-yes, N-no"
setlocal enabledelayedexpansion
Set "USB="
if errorlevel == 1 goto ONE
if errorlevel == 2 goto TWO
if errorlevel == 255 goto ERROR
:ONE
for /f "tokens=1-5" %%a in (
'wmic logicaldisk list brief'
) do if %%b Equ 2 if %%d gtr 0 Set USB=!USB! %%a
Echo:Found drive's:%USB%
set /p Drive=Choose drive:
if "%Drive%"=="" goto :ERROR
if not exist %drive%:\ goto :ERROR
if %drive% EQU C goto ERROR
if %drive% EQU D goto ERROR
cd /D %Drive%:
tree
:CHOICE
choice /c YN /t 15 /d N /m "Are you sure you want to delete all files? Y-yes, N-no"
if errorlevel == 1 goto DELETE
if errorlevel == 2 goto TWO
goto END
:TWO
echo "Program was cancelled"
goto END
:DELETE
del * /S /F /Q
rmdir /S /Q %Drive%:
echo "Files are deleted"
goto END
:ERROR
echo "There was an error"
goto end
:END
echo "Done"
pause
Here's an example, which includes a more thorough method of detecting USB drives.
#Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
For /F "Skip=2 Tokens=*" %%A In ('WMIC DiskDrive Where InterfaceType^="USB"^
Assoc /AssocClass:Win32_DiskDriveToDiskPartition 2^>Nul') Do (
For /F UseBackQ^ Delims^=^"^ Tokens^=2 %%B In ('%%A') Do (
For /F Delims^=^":^ Tokens^=6 %%C In (
'WMIC Path Win32_LogicalDiskToPartition^|Find "%%B"') Do (
For /F "Skip=1 Delims=" %%D In ('WMIC LogicalDisk Where^
"DeviceID='%%C:'" Get DeviceID^, VolumeName') Do Echo( %%D
Set "_C=!_C!%%C")))
If Not Defined _C Echo( You do not have a USB drive connected && GoTo :EndIt
If "%_C:~,1%" Equ "%_C%" GoTo :Picked
Echo( Enter the USB drive letter from the above [%_C%]:
For /F "Delims=? Tokens=2" %%A In ('Choice /C %_C%') Do Set "Letter=%%A:"
:Picked
If Not Defined Letter (Call :Task %_C%:) Else (Call :Task %Letter%)
:EndIt
>Nul Timeout 5
Exit/B
:Task
Choice /C YN /T 15 /D N /M "Do you want to delete all files from %1"
If ErrorLevel 2 Exit /B
REM Place your commands here for deleting from the selected drive
I think that the errorlevel's value will be replaced at the first if. I hope that this will help you:
#echo off
setlocal enabledelayedexpansion
Set "USB="
choice /c YN /t 15 /d n /m "Do you want to delete all files from USB drive? Y-yes, N-no"
set x=%errorlevel%
If %x%==1 goto ONE
If %x%==2 goto TWO
:ONE
for /f "tokens=1-5" %%a in (
'wmic logicaldisk list brief'
) do if %%b Equ 2 if %%d gtr 0 Set USB=!USB! %%a
Echo:Found drive's:%USB%
set /p Drive=Choose drive:
if "%Drive%"=="" goto ERROR
if not exist %drive%:\ goto :ERROR
if %drive% EQU C goto ERROR
if %drive% EQU D goto ERROR
cd /D %Drive%:
tree
:CHOICE
choice /c YN /t 15 /d N /m "Are you sure you want to delete all files? Y-yes, N-no"
Set x=%errorlevel%
if %x% == 1 goto DELETE
if %x% == 2 goto TWO
:TWO
echo "Program was cancelled"
goto END
:DELETE
del * /S /F /Q
rmdir /S /Q %Drive%:
echo "Files are deleted"
goto END
:ERROR
echo "There was an error"
goto end
:END
echo "Done"
pause
I'm in the process of making a script that selects 25 random tracks from a user specified folder and adds them into an .m3u playlist. The trouble I have is that my music folders include various other files as well (eg: .txt, .jpg, .png, .nfo, etc...)
So I'm looking for a way of excluding the extensions listed above, limiting the script to just work with audio files. Any help would be much appreciated!
Here is the code so far... It has been stitched together from various sources so accept my apologies if it is a little crude:
#echo off
title Multiple Choice Menu
:home
cls
echo.
echo Select a genre:
echo =============
echo.
echo 1) Jungle
echo 2) D'n'B
echo 3) Reggae
echo 4) Hip-Hop
echo 5) Exit
echo.
set /p genre=Type option:
if "%genre%"=="1" cd /D "D:\NL Safe 2.0\High Quality\Jungle"
if "%genre%"=="2" cd /D "D:\NL Safe 2.0\High Quality\DnB\Standard DnB"
if "%genre%"=="3" cd /D "D:\NL Safe 2.0\High Quality\Reggae
if "%genre%"=="4" cd /D "D:\NL Safe 2.0\High Quality\Hip-Hop"
if "%genre%"=="5" exit
:: Clear existing Playlist
#echo. > C:\Users\Brew\Desktop\random.m3u
:: Create numbered list of files in a temporary file
set "tempFile=%temp%\%~nx0_fileList_%time::=.%.txt"
dir /b /s /a-d %1 | findstr /n "^" >"%tempFile%"
:: Count the files
for /f %%N in ('type "%tempFile%" ^| find /c /v ""') do set cnt=%%N
:: Open 25 random files
for /l %%N in (1 1 25) do call :openRandomFile
:: Delete the temp file
del "%tempFile%"
:openRandomFile
set /a "randomNum=(%random% %% cnt) + 1"
for /f "tokens=1* delims=:" %%A in (
'findstr "^%randomNum%:" "%tempFile%"'
) do (
setlocal EnableDelayedExpansion
set tune=%%B
#echo !tune! >> C:\Users\Brew\Desktop\random.m3u
)
Assuming your song's folder is on %1.
The dir command supports wildcards in the idea that
dir *.txt *.xml
will only list .txt and .xml files.
So you can try doing this instead:
...
:: Create numbered list of files in a temporary file
set "tempFile=%temp%\%~nx0_fileList_%time::=.%.txt"
pushd %1
dir /b /s /a-d *.mp3 *.EXT1 *.EXT2 | findstr /n "^" >"%tempFile%"
popd
...
Where EXT will be the extensions you want to match.
This will create your %tempFile% with only the files you want. Since the code that selects a file ramdomly works, as I see, over this file, it won't need to change at all.
I couldn't get dir to work with both target directory and wildcards.
Hope it helps.
This is why i used pushd and popd.
Thanks Daniel,
I had another go at it an finally got something working on my own. I used a series of If statements to put the random file through an extension validation check
This is the working script
#echo off
title Multiple Choice Menu
:home
cls
echo.
echo Select a task:
echo =============
echo.
echo 1) Jungle
echo 2) D'n'B
echo 3) Reggae
echo 4) Hip-Hop
echo 5) Exit
echo.
set /p genre=Type option:
if "%genre%"=="1" cd /D "D:\NL Safe 2.0\High Quality\Jungle"
if "%genre%"=="2" cd /D "D:\NL Safe 2.0\High Quality\DnB\Standard DnB"
if "%genre%"=="3" cd /D "D:\NL Safe 2.0\High Quality\Reggae
if "%genre%"=="4" cd /D "D:\NL Safe 2.0\High Quality\Hip-Hop"
if "%genre%"=="5" exit
:: Clear existing Playlist
#echo. > C:\Users\Brew\Desktop\random.m3u
:: Create numbered list of files in a temporary file
set "tempFile=%temp%\%~nx0_fileList_%time::=.%.txt"
dir /b /s /a-d %1 | findstr /n "^" >"%tempFile%"
:: Count the files
for /f %%N in ('type "%tempFile%" ^| find /c /v ""') do set cnt=%%N
:openRandomFile
echo opening random file.....
set /a "randomNum=(%random% %% cnt) + 1"
for /f "tokens=1* delims=:" %%A in (
'findstr "^%randomNum%:" "%tempFile%"'
) do (
setlocal EnableDelayedExpansion
set tune=%%B
FOR %%i IN ("!tune!") do call :CheckifMusic
)
:CheckifMusic
echo checking now
FOR %%i IN ("!tune!") DO (
SET fileextension=%%~xi
IF !fileextension!==.aif goto ResultTrue
IF !fileextension!==.flac goto ResultTrue
IF !fileextension!==.m4a goto ResultTrue
IF !fileextension!==.mp3 goto ResultTrue
IF !fileextension!==.wav goto ResultTrue
IF !fileextension!==.wma goto ResultTrue
echo fail
echo !fileextension!
goto openRandomFile
:ResultTrue
echo pass
echo !fileextension!
#echo !tune! >> C:\Users\Brew\Desktop\random.m3u
set /A COUNTER=COUNTER+1
IF !COUNTER!==25 (
echo finished
pause
goto Done
) ELSE (
goto openRandomFile
)
:Done
echo.
:: Delete the temp file
del "%tempFile%"
exit
)
pause
Probably not the cleanest way to do it, but hey - It works!
replace
dir /b /s /a-d %1 | findstr /n "^" >"%tempFile%"
with
dir /b /s /a-d %1 | findstr /n /L /E /I /c:".mp3" /c:".wav">"%tempFile%"
Where the /c:".mp3"... can be extended as many times as you desire to select your target filetypes.
or
with
dir /b /s /a-d %1 | findstr /n /V /L /E /I /c:".jpg" /c:".txt">"%tempFile%"
Again repeating the /c:".jpg"... as many times as you desire to select your target exclude-me filetypes.
The /i means "case-insensitive", /e means "at the end of the line" /L means "literal match, not regex" and /v means "output non-matching"
i am trying to make a script to remove empty folders and delete files a number of days old. depending on what the txt file delimiters are set to. I have came up with this so far:
::Batch
SET CDID=%~dp0
SET TEST=TRUE
IF %TEST%==TRUE (
SET COMND1=ECHO
SET COMND2=ECHO
) ELSE (
SET COMND1=DEL
SET COMND2=RD
)
ECHO FILE RAN %date:~10%/%date:~4,2%/%date:~7,2% >>%CDID%\LOG.TXT
FOR /F "usebackq delims=| tokens=1,2" %%x IN (%CDID%PATH.txt) DO (
CALL :DEL_FOLDERS "%%x" %%y
CALL :DEL_FILES "%%x" %%y
)
GOTO :EOF
:DEL_FILES
FORFILES /p %1 /s /m *.* /d %2 /c "cmd /c %COMND1% #file"
GOTO :EOF
:DEL_FOLDERS
FOR /f "delims=" %%i in ('dir %%1 /s /b /ad ^| sort /r') do %COMND2% "%%i"
GOTO :EOF
::PATH.txt
C:\Temp\BLANK|10
C:\Temp\New folder|30
when i run the script #file will not populate and %%i will not populate, i am not sure what i am doing wrong. Help?
You made a couple of very small errors. In DEL_FOLDERS you used %%1 which meant that the argument was not expanded (you only needed one % here). You also did not handle the case where there are no files that match or the directories are empty. In the FORFILES command you put /m *.*; although the documentation says this is the default, the documentation is incorrect. Missing out the /m matches all files (the default) but by saying /m *.* you only match files with a dot!
My corrected version is:
::Batch
SET CDID=%~dp0
SET TEST=TRUE
IF %TEST%==TRUE (
SET COMND1=ECHO
SET COMND2=ECHO
) ELSE (
SET COMND1=DEL
SET COMND2=RD
)
ECHO FILE RAN %date:~10%/%date:~4,2%/%date:~7,2% >>%CDID%\LOG.TXT
FOR /F "usebackq delims=| tokens=1,2" %%x IN (%CDID%PATH.txt) DO (
CALL :DEL_FOLDERS "%%x" %%y
CALL :DEL_FILES "%%x" %%y
)
GOTO :EOF
:DEL_FILES
FORFILES /p %1 /s /d %2 /c "cmd /c %COMND1% #file" 2> nul
GOTO :EOF
:DEL_FOLDERS
FOR /f "delims=" %%i in ('dir "%~1" /s /b /ad 2^>nul ^| sort /r') do %COMND2% "%%i"
GOTO :EOF
::PATH.txt
C:\Temp\BLANK|10
C:\Temp\New folder|30