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
Related
I have some pdf's in a folder that I need to organize them like this:
PDF name: 123.12.123.pdf ; 102030_01.pdf; 102030_02.pdf; 123.4512.34561.23412.pdf
Now I need to create folders with the filename (without the characters removed, ex: 12345123456123412) and rename them to the following pattern: ex: P12345123456123412_V1_A0V0_T07-54-369-664_S00001.pdf
for this I have used the following code which works very well:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
If "%~1" == "" GoTo :EOF
For %%G In (%*) Do (For %%H In ("%%~G") Do If "%%~aH" Lss "-" (
Echo Error! %%G no longer exists.
%SystemRoot%\System32\timeout.exe /T 2 /NoBreak 1>NUL
) Else If "%%~aH" GEq "d" (For %%I In ("%%~G\*.pdf") Do Call :Sub "%%~I"
) Else If /I "%%~xG" == ".pdf" (Call :Sub "%%~G"
) Else (Echo Error! %%G is not a PDF
%SystemRoot%\System32\timeout.exe /T 2 /NoBreak 1>NUL))
GoTo :EOF
:Sub
Set "basename=%~n1"
Set "basename=%basename:.=%"
MD "%~dp1%~n1" 2>NUL
If Not ErrorLevel 1 Move /Y %1 "%~dp1%~n1\P%basename:-=%_V1_A0V0_T07-54-369-664_S00001_Volume%~x1"
Exit /B
I drag the pdfs into the .bat and it does the adjustment.
It happens that there is a case that I am not able to handle. Some pdfs need to be in the same folder, for example in the following case:
PDF name: 102030_01.pdf; 102030_02.pdf;
Note that the pdfs have the same number, only after the _ that we have the difference. In this case you would need to create a folder with the name:102030
And move the two files into it, modifying their name as follows:
102030_01.pdf -> P102030_V1_A0V0_T07-54-369-664_S00001.pdf
102030_02.pdf -> P102030_V1_A0V0_T07-54-369-664_S00002.pdf
Could anyone help?
:Sub
Set "basename=%~n1"
Set "basename=%basename:.=%"
if /i "%basename%" neq "%basename:_=%" goto sub2
MD "%~dp1%~n1" 2>NUL
If Not ErrorLevel 1 Move /Y %1 "%~dp1%~n1\P%basename:-=%_V1_A0V0_T07-54-369-664_S00001_Volume%~x1"
Exit /B
:sub2
for /f "tokens=1*delims=_" %%b in ("%basename%") do (
MD "%~dp1%%b" 2>NUL
ECHO Move /Y %1 "%~dp1%%b\P%basename:-=%_V1_A0V0_T07-54-369-664_S000%%c_Volume%~x1"
)
Exit /B
Always test on dummy data first.
This code echoes the proposed move. After verification, remove the echo keyword to activate.
Caution: My reading of the code is that - should be removed from the basename in the new name, and that _Volume should be appended to the name part, which is not shown in your examples.
Essentially, if the basename contains _ then goto sub2.
sub2 partitions the name in basename, assigning the first part to %%b and the second to %%c (See for /? from the prompt for documentation)
Then the directory is created
The md will object if the directory already exists, hence the 2>nul in the original code (suppresses error messages)
If md found that error in the original then this appears to be a problem, so the move is not executed. In the new version, it is expected that the directory may already exist, so the errorlevel processing has been removed.
I am trying to create a menu with submenus which their names without extension come from the directory. However, I am unable to make a variable for choice as number. This code does not work anyhow. I would also want to display a number at the beginning of each file name in the menu; in fact, number of the files will also be one of the number that user selects as input. I could not overcome the problem.
#echo off
cd C:\Users\Murray\Documents\ConfigFiles\
for /f %%A in ('dir /a-d-s-h /b *conf ^| find /v /c ""') do set count=%%A
echo File count = %count%
for %%F in ("C:\Users\Murray\Documents\ConfigFiles\*.conf") do echo %%~nxF
set choice=
set /C /N="Please choice: "
if "%choice%" == "%count%" goto SUBMENU
if NOT EXIST "C:\Users\Murray\Documents\ConfigFiles\%choice%" goto NOFILE
:SUBMENU
Echo You are here
goto end
:NOFILE
echo %choice% could not be found.
goto END
:end
Any help will be appreciated.
Here's a quick example of a method touted in the answer by on no. It will print a number to the screen followed by the names of each file matching file extension, so will allow a large number of matching files, (although the end user may have to scroll a long list). Once a file is chosen, your end user then just types its corresponding number. The code should not proceed beyond the input request until a number matching one in the list is ENTERed.
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "src=C:\Users\Murray\Documents\ConfigFiles"
Set "ext=.config"
If Not Exist "%src%\*%ext%" Echo No file matches.& GoTo End
For /F "Delims==" %%G In ('"(Set #) 2>NUL"') Do Set "%%G="
For /F "Delims==" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe OS Call /? ^|
%SystemRoot%\System32\find.exe "=="') Do Set "HT=%%G"
For /F "Tokens=1* Delims=:" %%G In ('Dir /B /A:-D "%src%\*%ext%" 2^>NUL ^|
%SystemRoot%\System32\findstr.exe /E /I /L /N "%ext%"'
) Do Set "#%%G=%%H" & Echo( %%G.%HT:~-1%%%H
If Not Defined #1 Echo No file matches.& GoTo End
:Opt
Echo(
Set "HT=" & Set "opt="
Set /P "opt=Enter the number for your chosen file>"
If Not Defined opt (GoTo Opt) Else Set "opt=%opt:"=%"
Set # | %SystemRoot%\System32\findstr.exe /B /L "#%opt%=" 1>NUL || GoTo Opt
SetLocal EnableDelayedExpansion
For %%G In ("!#%opt%!") Do EndLocal & Set "opt=%%~G"
#Rem Your code goes below here
Echo(& Echo You Selected %opt%
#Rem Your code ends above here
:End
Setlocal EnableDelayedExpansion & For /F "Tokens=1,2" %%G In ("!CMDCMDLINE!"
) Do Endlocal & If /I "%%~nG" == "cmd" If /I "%%~H" == "/c" Echo(& Echo Press^
any key to exit.&Pause 1>NUL
All you need to do is to modify the variables values on lines 4 and 5, if necessary, in order to test it. I will not be supporting changes or additions beyond that. Once you have tested the code, you may insert your code between lines 25 and 29, replacing the example line I left there for your test.
Batch's limitations are to blame for sub-par corrections to this question.
Here's the best I could whip up in a few seconds:
#echo off
:one
cls
cd C:\Users\Murray\Documents\ConfigFiles\
for %%F in ("C:\Users\Murray\Documents\ConfigFiles\*.conf") do echo %%~nxF
echo.
echo Enter Configuration Name:
set/p "prompt=>"
if exist %prompt%.txt goto :two
if exist %prompt% goto :two
cls
echo File not found
pause >NUL
goto :one
:two
cls
REM When file is found, this code will run
pause >NUL
The set command does not prompt for user input unless specified with the /p switch. To make your code more friendly, i'd also recommend to prompt for the filepath earlier on in the code.
EDIT: A few alternative solutions: Declare the 9th option of the prompt of the choice command as a "second page" or "more" option. This would really be a pain for the user in a directory with tens or hundreds of files. Another; you can assign an integer to each file that matches your quasi-query and echo them before each filename on the screen, then allow the user to input the number to get that file. That seems fairly efficient, and if you'd like to explore one or both of those alternates I can help (if you need it).
need a bit of help with a project.
Scenario :-
Manually migrating PC's to new Domain breaks a piece of software due to the new profile not having the settings from the old user profile - required to make it run. The old profile is called one thing, the new one - something else e.g.
C:\Users\Sue.Barnes.B3209 (old) C:\Users\Susan.Barnes.NGP (new)
Within the %localappdata% of each (old) profile there "maybe" a config file for this software, if the user has used it previously. E.g.
C:\Users\Sue.Barnes.B3209\AppData\Local\Acme_Limited\Acme.exe_Url_m5l4ujc5t22f3qw0q5uz1dwlfcnrdaoh\1.0.0.15\user.config
Objectives :-
I want to be able to scan through the C:\Users Folder and report back the full path, name & extension of file where it is found, then have the list set in a menu choice where upon input choice will copy the user.config file to the exact same location but in the currently logged on new profile appdata folder.
e.g.
Enumerating List Of User Profiles in the System...
Admin
Public
User
From the Profiles detecting active Configs...
C:\Users\Admin\AppData\Local\Acme_Limited\Acme.exe_Url_m5l4ujc5t22f3qw0q5uz1dwlfcnrdaoh\1.0.0.15\user.config
C:\Users\Public\AppData\Local\Acme_Limited\Acme.exe_Url_m5l4ujc5t22f3qw0q5uz1dwlfcnrdaoh\1.0.0.15\user.config
Press any key to continue . . .
However it is at this point I'm out of my depth. I cant seem to be able to create a choices menu of each instance where the file is found. However many are found, I'd like a number to be printed on the screen so I can choose the correct config to copy to the newly set up profile. Simply,
Xcopy /f C:\Users\Sue.Barnes.B3209\AppData\Local\Acme_Limited\Acme.exe_Url_m5l4ujc5t22f3qw0q5uz1dwlfcnrdaoh\1.0.0.15\user.config
%localappdata%\Acme_Limited\Acme.exe_Url_m5l4ujc5t22f3qw0q5uz1dwlfcnrdaoh\1.0.0.15\
Current code base :-
echo.
echo Enumerating List Of User Profiles in the System...
echo.
TIMEOUT /T 3 >NUL
dir /b C:\Users
echo.
echo From the Profiles detecting active Configs...
echo.
TIMEOUT /T 3 >NUL
for /R "C:\Users" %%a in (1.0.0.15\user.config*) do (
echo %%~dpnxa
)
)
echo.
pause
Any help be greatly appreciated, thanks
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
set "dontshow=u:\dontshow.txt"
set "configs=u:\configs.txt"
set "profiles=u:\profiles.txt"
:again
echo :>>"%dontshow%"
del "%configs%" >nul 2>nul
del "%profiles%" >nul 2>nul
:: Find all of the "user.config" files having ..\appdata\local\Acme_limited\...1.0.0.15
FOR /f "tokens=1-9delims=\" %%a IN (
'dir /s /b /a-d "%sourcedir%\user.config" '
) DO if /i "%%d"=="appdata" if /i "%%e"=="local" if /i "%%f"=="Acme_Limited" if /i "%%h"=="1.0.0.15" (
rem these are the users' config files that may be copied.
echo %%c|findstr /x /g:"%dontshow%" /i /L >nul
if errorlevel 1 >>"%configs%" echo %%c
)
:: Find all of the user profiles that are not in "dontshow" and do NOT have "user.config" - These are the profiles that may need to be processed
FOR /f "delims=" %%a IN (
'dir /b /ad "%sourcedir%" '
) DO (
echo %%a|findstr /x /g:"%dontshow%" /i /L >nul
if errorlevel 1 echo %%a|findstr /x /g:"%configs%" /i /L >nul
if errorlevel 1 >>"%profiles%" echo %%a
)
:: Now we have a list of profiles with "user.config" missing in %profiles%
:: And possible source-profiles in %configs%
:: Show the list of "config missing" profiles
cls
findstr /n /v /L /c:":" "%profiles%"
:reselectd
set "selectiond="
set /p "selectiond=Choose destination profile "
if not defined selectiond goto :eof
for /f "tokens=1*delims=:" %%a in ('findstr /n /v /L /c:":" "%profiles%"') do if "%%a"=="%selectiond%" set "selectiond=%%b"&goto selects
echo Invalid selection
goto reselectd
:selects
for /L %%a in (1,1,4) do echo.
findstr /n /v /L /c:":" "%configs%"
:reselects
set "selections="
set /p "selections=Choose source profile "
if not defined selections goto :eof
for /f "tokens=1*delims=:" %%a in ('findstr /n /v /L /c:":" "%configs%"') do if "%%a"=="%selections%" set "selections=%%b"&goto process
echo Invalid selection
goto reselects
:process
echo Copy from profile "%selections%" to "%selectiond%" ?
choice
if errorlevel 2 goto again
:: Do the copy... just echoed, remove `echo` keyword to activate
ECHO md "%sourcedir%\%selectiond%\appdata\localAcme_Limited\whatever\1.0.0.15"
ECHO copy "%sourcedir%\%selections%\appdata\localAcme_Limited\whatever\1.0.0.15\user.config" "%sourcedir%\%selectiond%\appdata\localAcme_Limited\whatever\1.0.0.15"
choice /M "Add '%selections%' to processed file ? "
if errorlevel 2 goto processedd
>>"%dontshow%" echo %selections%
:processedd
choice /M "Add '%selectiond%' to processed file ? "
if errorlevel 2 goto again
>>"%dontshow%" echo %selectiond%
goto again
Interesting exercise. Lack of information about what Acme.exe_Url_m5l4ujc5t22f3qw0q5uz1dwlfcnrdaoh is about (constant string, change-per-user? What?), though - left as an exercise for OP to resolve.
You would need to change the setting of sourcedir to suit your circumstances. The listing uses a setting that suits my system.
I decided that the key part is the third directory level - the user-id. This approach uses files of user-id, two are temporary and one is permanent - "dontshow.txt" which contains a list of user-ids NOT to show. This can be manually maintained using an editor if required.
So - to start, add a line containing a single colon to the "dontshow" file. This is a dummy which can never match a real user-id and is required because findstr doesn't like an empty dictionary file.
The first step is to find all of the user.config files, and use for /f to process the filenames found, selecting the significant directory-levels. Having excluded any path that does not fit the criteria, the third level contents is matched against the exclusions in "dontshow.txt". If the name found is new, add it to the configs file.
Second verse - same as the first, but this time only looking at the directory names. Note that the entry is made to profiles if the name found is NOT on the dontshow list AND is also NOT on the config list.
Now select a destination profile by listing profiles and appending a prefix linenumber: using findstr. Enter a number (manual entry, unchecked) and then match the line number by using for/f.
Rinse and repeat for the configs file for the source profile.
Display the proposed command, accept a Y/N response then ask whether each name should be added to the dontshow list.
And straight on 'til morning.
Note that responding with Return to any of the set /p commands will exit the batch.
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 just starting to get my feet wet with MS Batch files. I have created a small batch that searches the entered directory for files containing a certain string using findstr /m. It is returning a file that contains the string, but only the first one it finds. I have searched the findstr /? and online command reference, as well as this site. I cannot find a way for findstr to return ALL the files with an instance of the string. What am I missing?
#echo off
setlocal
ECHO This Program Searches for words inside files!
:Search
set /P userin=Enter Search Term:
set /p userpath=Enter File Path:
FOR /F %%i in ('findstr /M /S /I /P /C:%userin% %userpath%\*.*') do SET finame=%%i
if "%finame%" == "" (set finame=No matching files found)
echo %finame%
set finame=
endlocal
:searchagain
set /p userin=Do you want to search for another file? (Y/N):
if /I "%userin%" == "Y" GOTO Search
if /I "%userin%" == "N" GOTO :EOF ELSE (
GOTO wronginput
)
Pause
:wronginput
ECHO You have selected a choice that is unavailable
GOTO searchagain
If you replace this:
FOR /F %%i in ('findstr /M /S /I /P /C:%userin% %userpath%\*.*') do SET finame=%%i
if "%finame%" == "" (set finame=No matching files found)
echo %finame%
set finame=
with this then it may work the way you expect
findstr /M /S /I /P /C:"%userin%" "%userpath%\*.*"
if errorlevel 1 echo No matching files found
Thanks everyone. Just in case someone else searches this site, here is my final code.
#echo off
ECHO This Program Searches for words inside files!
:Search
setlocal
set /P userin=Enter Search Term:
set /p userpath=Enter File Path:
findstr /M /S /I /P /C:%userin% %userpath%\*.* 2> NUL
if ERRORLEVEL 1 (ECHO No Matching Files found) ELSE (
GOTO searchagain
)
endlocal
:searchagain
setlocal
set /p userin=Do you want to search for another file? (Y/N):
if /I "%userin%" == "Y" GOTO Search
if /I "%userin%" == "N" GOTO :EOF ELSE (
GOTO wronginput
)
endlocal
:wronginput
ECHO You have selected a choice that is unavailable
GOTO searchagain
In your for loop, when you assign to finame the value of %%i, you are replacing the previous value, so only the last file gets echoed to console.
If you try your findstr command (out of the for) you will see the list of files.
to place all in var:
setlocal enabledelayedexpansion
set nlm=^
set nl=^^^%nlm%%nlm%^%nlm%%nlm%
for %%i in ('dir %userpath% /b') do for /f %%a in ('type "%userpath%\%%i"^|find "%userin%" /i') do set out=!out!!nl!%%i