Second latest folder in a Directory - windows

I want a batch file which will find out which is the second latest folder created/modified in a directory.
I found this article but no matter how much i tried i could not understand how it works
#echo off
set "root_dir=c:\somewhere"
pushd "%root_dir%"
set "bl1="
set "bl2="
setlocal enableDelayedExpansion
for /f "tokens=* delims=" %%# in ('dir /b /a:-d /o:d') do (
set "bl2=!bl1!"
set "bl1=%%#"
)
echo %bl2%
endlocal
If i use it as it is then i can get the second latest folder but this script is supposedly able to get which ever latest folder you need , be it 1st or nth.
Could someone please tell me what modifications need to be done to the script to accomplish that. Also how exactly this script works

In your approach, the latest folder is already available in variable bl1; add echo %bl1% at the end before endlocal to display it. Retrieving the nth folder is simply not possible in a flexible way with that script as you would need to define another variable (say bl3, bl4,..., bln) within the loop.
However, you could reverse the sort order of the output of the dir command by changing the /O option, so it returns the latest (most recent) item first. Then let an index number count the iterations of the loop, and if that index equals the predefined number n, store the currently iterated item:
#echo off
setlocal EnableDelayedExpansion
rem // Define N here to get Nth-latest folder:
set /A LATEST=2
set /A INDEX=0
for /F "eol=| delims=" %%# in ('dir /B /A:D /O:-D "C:\somewhere"') do (
set /A INDEX+=1
if !INDEX! EQU !LATEST! (
set "ITEM=%%#"
)
)
if defined ITEM echo %LATEST%th-latest folder: %ITEM%
endlocal
exit /B
Update
Here is a modified script with the following improvements:
Exclamation marks ! in folder names are no longer lost due to toggling delayed expansion;
the target directory can be provided as the first command line argument; if omitted, the current directory is used;
the number n can be given as the second command line argument; if omitted, the user is prompted for it (this addresses elzooilogico's comment); n defaults to 1 for empty input;
the display output is improved to avoid something weird like 1th-latest, 2th-latest and 3th-latest; instead, The latest, 2nd-latest and 3rd-latest is returned, respectively;
So this is the code:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem /* define location path and folder pattern as 1st command line argument;
rem /* Define number N as 2nd command line argument to get Nth-latest folder. */
set "LATEST=%~2"
set /A LATEST+=0
if %LATEST% LEQ 0 (set /P LATEST="Enter N [1]: " & set /A LATEST+=0)
if %LATEST% LEQ 0 set /A LATEST=1
set /A INDEX=0
for /F "eol=| delims=" %%# in ('dir /B /A:D /O:-D "%~1"') do (
set /A INDEX+=1
setlocal EnableDelayedExpansion
if !INDEX! EQU !LATEST! (
endlocal
set "ITEM=%%#"
goto :SKIP & rem // break loop after having retrieved Nth-latest folder;
) else endlocal
)
:SKIP
setlocal EnableDelayedExpansion
if defined ITEM (
if %LATEST% EQU 1 (echo The latest file: !ITEM!) else (
if %LATEST% EQU 2 (echo 2nd-latest file: !ITEM!) else (
if %LATEST% EQU 3 (echo 3rd-latest file: !ITEM!) else (
echo %LATEST%th-latest file: !ITEM!)))
)
endlocal
endlocal
exit /B
To achieve a similar result as with the simple script on top of this answer, you need to call this script by the following command line, supposing it has been saved as Nth-latest.bat:
Nth-latest.bat "C:\somewhere" 2

Related

Keep X amount of files in folder, forfiles

I would like to keep the X latest files from a folder and delete the rest. Is this possible with FORFILES? If it's not I can fallback to another solution I seen here. Thanks for help.
I did this but it takes by dates: EDIT
forfiles /p [path] /s /d -5 /c "cmd /c echo #file"
(echo file for testing purpose)
#ECHO OFF
SETLOCAL
SET "targetdir=U:\destdir"
SET /a retain=10
FOR /f "skip=%retain%delims=" %%a IN (
'dir /b /a-d /o-d "%targetdir%\*" '
) DO ECHO (DEL "%targetdir%\%%a"
GOTO :EOF
You would need to change the setting of targetdir to suit your circumstances. Equally, this procedure targets all files - change the filemask to suit.
The required DEL commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(DEL to DEL to actually delete the files.
method is to simply execute a dir in basic form without directories, sorted in reverse-date order.
Skip the first 10 entries, and delete the rest.
With forfiles I see no chance to accomplish your task of returning the newest (most recent) number of files.
So my idea for this approach is this:
to use dir /B /A:-D /T:C /O:-D to retrieve a bare list (/B) of files (no directories, /A:-D), sorted by creation date (/T:C; if you want to use the last modification date, simply remove the /T:C portion) in decending order (/O:-D), meaning newest items first;
to put over a for /F "eol=| delims=" loop to gather and parse the dir output line by line, meaning file by file, not excluding file names beginning with ; (eol=|, | is illegal for file names) and not splitting file names containing white-spaces like SPACE or TAB (delims=);
to establish a variable that constitutes a counter, incremented per each loop iteration;
to place an if condition inside of the loop to check if the counter reached the desired limit number and in case it is fulfilled, to break the for /F loop by goto;
Here is the related code:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Define global constants here:
set "TARGETPATH=\path\to\files\*.*"
set /A "TOTAL=10"
set /A "COUNT=0"
for /F "eol=| delims=" %%F in ('
dir /B /A:-D /T:C /O:-D "%TARGETPATH%"
') do (
echo(%%F
set /A COUNT+=1
setlocal EnableDelayedExpansion
if !COUNT! GEQ %TOTAL% (
endlocal
goto :NEXT
) else (
endlocal
)
)
:NEXT
endlocal
exit /B
I toggled the delayed variable expansion within the for /F loop to avoid trouble in case file names contain exclamation marks !, which would get lost in the line echo(%%F in case it is on.
Update
The following code accomplishes the original task of your question, namely to delete files in a given directory but to keep the most recent number of files:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Define global constants here:
set "TARGETPATH=\path\to\files\*.*"
set /A "TOTAL=10"
set "SKIPOPT=" & if %TOTAL% GTR 0 set "SKIPOPT=skip=%TOTAL% "
for /F "%SKIPOPT%eol=| delims=" %%F in ('
dir /B /A:-D /T:C /O:-D "%TARGETPATH%"
') do (
del /P "%%F"
)
endlocal
exit /B
Since for /F supports a skip= to skip the given number of lines, and so files in our situation, let us make use of it. It is given indirectly here via variable SKIPOPT, which holds the entire option string like skip=10 (given that TOTAL is set to 10). The if %TOTAL% GTR 0 query is implemented for the script not to fail in case TOTAL is 0, because for /F does not accept the option skip=0.
The /P switch at the del command lets appear a prompt Delete (Y/N)? for testing purposes. If you do not want any prompts, simply remove it.

Batch script issues when copying files

I'm having an issue and I have been working on it for hours. I have no idea why it wont work, but I have a feeling it has to do with doing a for loop in a for loop. Its going to be a little difficult to explain. So here goes:
I have this code that works. What it does is copy 3 files from programdata to appdata. We need the "for loop" because the profile %var% is always different in that folder. It will find the name and input it to "cd" into.
Echo Restore Firefox Files
set copycmd=/y
timeout 7
c:
cd "%userprofile%\AppData\Roaming\Mozilla\Firefox\"
for /f "tokens=1,2 delims=/" %%i in ('findstr /l ".default" profiles.ini') do call set var1=%%j
cd profiles\%var1%
if exist "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%var1%\places.sqlite" copy "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%var1%\places.sqlite"
if exist "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%var1%\key3.db" copy "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%var1%\key3.db"
if exist "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%var1%\signons.sqlite" copy "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%var1%\signons.sqlite"
The point is, that code works. It copys from where i want it to. Here's my issue. I have this code. It will not work. Note the main loop starts where it says "Echo Loop through each user and copy files".
Echo Set Usernames into Variables (User1, User2, etc)
for /F "tokens=*" %%A in (C:\ProgramData\TLCloud\WindowsUsers.txt) do (
SET /A vidx=!vidx! + 1
set user!vidx!=%%A
set /A i = !i! + 1
)
Echo Put Usernames into an Array
set i=0
for /f "delims=" %%a in (C:\ProgramData\TLCloud\WindowsUsers.txt) do (
set /A i+=1
set list[!i!]=%%a
)
Echo Loop Through Each User and Copy Files
for /F "tokens=2 delims==" %%s in ('set list[') do (
If NOT "%%s" == "Guest" (
If NOT "%%s" == "Administrator" (
Echo Restore Firefox Files
set copycmd=/y
::start "browser" /d "C:\Program Files (x86)\Mozilla Firefox" firefox.exe
timeout 7
c:
cd "%userprofile%\AppData\Roaming\Mozilla\Firefox\"
for /f "tokens=1,2 delims=/" %%i in ('findstr /l ".default" profiles.ini') do call set var1=%%j
cd profiles\%var1%
if exist "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%var1%\places.sqlite" copy "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%var1%\places.sqlite"
if exist "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%var1%\key3.db" copy "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%var1%\key3.db"
if exist "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%var1%\signons.sqlite" copy "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%var1%\signons.sqlite"
)
)
)
Why will this code not work? Ignore the variables and array code, that is there so instead of "Username" I can eventually use "%%s" as the variable for changing usernames. But there is no reason this should not work, or am I not allowed to have a for loop in another for loop?
Any help is greatly appreciated.
%var1% is replaced by an empty string already on starting processing the most outer loop with set list[
One solution would be using !var1! instead of %var1% as it looks like delayed environment variable expansion is already enabled. In this case call set var1=%%j should be also corrected to just set var1=%%j.
Another solution is using following code:
Echo Set Usernames into Variables (User1, User2, etc)
for /F "tokens=*" %%A in (C:\ProgramData\TLCloud\WindowsUsers.txt) do (
SET /A vidx=!vidx! + 1
set user!vidx!=%%A
set /A i = !i! + 1
)
Echo Put Usernames into an Array
set i=0
for /f "delims=" %%a in (C:\ProgramData\TLCloud\WindowsUsers.txt) do (
set /A i+=1
set list[!i!]=%%a
)
Echo Loop Through Each User and Copy Files
for /F "tokens=2 delims==" %%s in ('set list[') do (
If NOT "%%s" == "Guest" (
If NOT "%%s" == "Administrator" (
Echo Restore Firefox Files
set copycmd=/y
rem start "browser" /d "C:\Program Files (x86)\Mozilla Firefox" firefox.exe
rem timeout 7
cd /D "%userprofile%\AppData\Roaming\Mozilla\Firefox\"
for /f "tokens=1,2 delims=/" %%i in ('findstr /l ".default" profiles.ini') do (
cd "profiles\%%j"
if exist "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%%j\places.sqlite" copy "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%%j\places.sqlite"
if exist "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%%j\key3.db" copy "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%%j\key3.db"
if exist "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%%j\signons.sqlite" copy "C:\Programdata\TLCloud\BrowserBackup\Username\MozillaRoaming\Firefox\Profiles\%%j\signons.sqlite"
)
)
)
)
Two problems.
The :: style commenting is a broken label. It causes FOR to get confused and terminate. Replace it with REM within a block statement (a parenthesised series of statements).
Second is more major, but easily cured.
You haven't shown us your entire batch. It would follow from such statement as set list[!i!]=%%a that you have delayed expansion turned ON. This is excruciatingly important.
Within a block statement, any %var% is evaluated and replaced at parse time, so %var1% will be replaced by var1's value when the entire block (ie the outermost FOR is parsed. Since you are attempting to change var1 within your innermost loop, to access the changed value, you need to use !var1! which means the current, not the parse-time value of the variable.
Now, since delayed expansion appears to be enabled, simply replacing %var1% with !var1! would appear to be a solution. It's not hard to notice however that var1 is being asigned the value %%j so there appears to be no reason to use var1 at all - simply replace it with %%j!

Using FOR loop to copy code between to labels in batch file

Ok so I write in batch files a lot. A while back I asked a question user:cmd on how to copy one part of a running batch file into a new batch file,
Well it works if your going to use it one time in a batch file. My goal is to create multiple large batch files from within a single setup batch. What happens is if they choose to install, then the batch file runs the following.
cls
setlocal EnableDelayedExpansion
color e
::Start of embedded code
set Begin=
for /F "delims=:" %%a in ('findstr /N "^:EMBEDDED_CODE" "%~F0"') do (
if not defined Begin (
set Begin=%%a
) else (
set End=%%a
)
)
::*****************************************************************************
(for /F "skip=%Begin% tokens=1* delims=[]" %%a in ('find /N /V "" "%~F0"') do (
if %%a equ %End% goto :Build-file2
echo(%%b
)) > file1.bat & goto :Build-file2
)
goto :Build-file2
:EMBEDDED_CODE Begin
CODE TO PUT INTO "file1.bat"
:EMBEDDED_CODE End
:Build-file2
cls
setlocal EnableDelayedExpansion
color e
::Start of embedded code
set Begin=
for /F "delims=:" %%a in ('findstr /N "^:EMBEDDED_CODE" "%~F0"') do (
if not defined Begin (
set Begin=%%a
) else (
set End=%%a
)
)
::*****************************************************************************
(for /F "skip=%Begin% tokens=1* delims=[]" %%a in ('find /N /V "" "%~F0"') do (
if %%a equ %End% goto :EOF
echo(%%b
)) > file2.bat & goto :EOF
)
goto :EOF
:EMBEDDED_CODE Begin
CODE TO PUT INTO "file2.bat"
:EMBEDDED_CODE End
The problem that is occurring is instead of it just copying the code between labels EMBEDDED_CODE Begin and EMBEDDED_CODE End in the first FOR loop it copies from EMBEDDED_CODE Begin down to the very bottom of the script puts it in the file I want and then goes to the next FOR loop which repeats the process with different code between the to labels. so file1.bat and file2.bat both contain the exact same code but with the desired file names of file1.bat AND file2.bat.
Why would you expect anything different than the results you are getting? The FINDSTR will search the entire file, so Begin is set to the first occurrence of :EMBEDDED_CODE in the first block of code, and End is set to the last occurrence in the last block of code (last value set wins). You replicate the code, so of course you get the same faulty result two times.
Simply change the labels in your second block of code, perhaps :EMBEDDED_CODE2, and adjust your 2nd FINDSTR accordingly. All should work then.
I often use a slightly different approach that minimizes the amount of file reading. Simply modify all lines from a given embedded block of code with the same unique prefix. Then FINDSTR can directly output the desired lines, and a FOR /F is used to strip off the prefix. You just need the prefix to end with a character that never matches the beginning of your code.
You should be careful about enabling delayed expansion when reading a file with FOR /F. Your embedded code will be corrupted if it contains ! and delayed expansion is enabled. (unless the ! is escaped, but that can be a pain)
#echo off
for %%C in (1 2) do (
for /f "tokens=1* delims=}" %%A in ('findstr /bl ":%%C}" "%~f0"') do echo(%%B
)>file%%C.bat
:1}Your first code block goes here
:1}
:1} Blank lines and indents are preserved
:1}And so are exclamation points!
:2}And here is your second code block
:2}...
echo file1.bat
echo ---------
type file1.bat
echo(
echo(
echo file2.bat
echo ---------
type file2.bat
This will almost do what you need.
This code needs to read twice the input file, first to locate the range of lines to process (findstr line numbering), and second to extract them. In second loop findstr numbering is used again to avoid for /f to compress blank lines and alter line numbering.
On the other hand, the problem with special characters inside extacted text is handled, enabling and disabling delayed expansion as needed.
Maybe not the best performance, but it seems to work. Adapt as needed.
#echo off
setlocal enableextensions enabledelayedexpansion
call :extractEmbedded "Section1" extracted.txt
if not errorlevel 1 (
cls
type extracted.txt
)
exit /b
:extractEmbedded id outputFile
rem prepare environment
setlocal enableextensions enabledelayedexpansion
rem asume failure on execution
set "_return=1"
rem find embedded zone in current file
set "_start="
set "_end="
for /f "tokens=1 delims=:" %%l in ('findstr /n /b /c:":EMBEDDED %~1" "%~f0"') do (
if not defined _start ( set "_start=%%l" ) else ( set "_end=%%l" )
)
rem adjust lines to process
set /a "_start+=0"
set /a "_end-=1"
rem if nothing found, task done
if %_start% GEQ %_end% goto endExtractEmbedded
rem prepare file extraction
if "%_start%"=="0" (set "_skip=" ) else ( set "_skip=skip^=%_start%" )
rem extract proper area of file to output file
(for /f tokens^=^*^ %_skip%^ eol^= %%l in ('findstr /n "^" "%~f0"') do if !_start! LSS !_end! (
setlocal disabledelayedexpansion
set "_line=%%l"
setlocal enabledelayedexpansion
echo(!_line:*:=!
endlocal & endlocal
set /a "_start+=1"
))>"%~2"
rem everything ok
set "_return=0"
:endExtractEmbedded
rem exit with errorlevel
endlocal & exit /b %_return%
:EMBEDDED Section1
This is a section; of embedded!!! code
that needs to be extracted to generate
a new file to be processed.
TEST: !""$%&/()=?¿^*[];,:-\|
:EMBEDDED Section1

Batch File to list folders and allow user selection

I have searched and searched to find a solution to (what feels like) a unique problem. Many
answers here have been quite helpful and have gotten me a long way, but the last bit has me stumped.
My batch file needs to open an executable in a folder with a variable name
This folder may be in one of two directories
This is what I originally had and it works
#ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET DIR1="%USERPROFILE%\AppData\Local\Application Workspace\Profiles"
SET DIR2=C:\Application\SRW\Profiles
IF NOT EXIST %DIR1% (
GOTO PROFILE_2
) ELSE (
GOTO PROFILE_1
)
:PROFILE_1
for /f "delims=" %%A in ('dir %DIR1% /ad /b') do (
set foldername=%%~nxA
)
SET DIR1=%DIR1:"=%
SET DIR=%DIR1%\%foldername%\app.exe
GOTO START_APP
:PROFILE_2
for /f "delims=" %%A in ('dir %DIR2% /ad /b') do (
set foldername=%%~nxA
)
SET DIR=%DIR2%\%foldername%\app.exe
GOTO START_APP
:START_APP
START "" /b "%DIR%"
:EOF
ENDLOCAL
EXIT
Then I was thrown a curveball when I discovered that some users may have multiple profiles in the variable profile folder and they need to be able to select which one to use for that given task. Now I have a variable profile folder and either one or multiple variable profiles within that folder.
I have found code to list the folder names and display them in the command window for selection.
#echo off
cls
setlocal EnableDelayedExpansion
set /a count=0
for /d %%d in (*) do (
set /a count+=1
#echo !count!. %%d
)
setlocal DisableDelayedExpansion
set /P selection="select folder number:"
I also found this routine which allows the user to select a file from a list then it is supposed to translate that file name into a variable.
Batch Script Programming -- How to allow a user to select a file by number from a list of files in a folder?
Unfortunately, I cannot get the example in the link to work as is and I have no idea how to make it work with folder names as the folder name example and the file name example are close but not close enough for me to understand what to do. And even if it somehow does manage to work, how then can I make such a routine work within the original code posted above?
In addition, I really don't want the user to be forced to make a folder selection if there is only one folder. If only one folder exists, it should be placed into the folder name variable automatically and nothing displays to the user at all.
Is what I'm trying to do even possible at all?
Any help is most appreciated.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
::
:: Set count-of-targets and application name
::
SET /a targets=0
SET appname=app.exe
SET "dots=..."
::
:: clear all "$" variables
::
FOR /f "delims==" %%i IN ('set $ 2^>nul') DO SET "%%i="
::
:: look for app.exe in (1) userprofile... (2) \application\srw...
::
SET dir1="%USERPROFILE%\AppData\Local\Application Workspace\Profiles"
SET dir2="C:\Application\SRW\Profiles"
:: My testing - my directory names
SET dir1="c:\sourcedir"
SET dir2="C:\destdir\test dir"
FOR %%s IN (%dir1% %dir2%) DO (
FOR /f "delims=" %%i IN ('dir /s /b "%%~s\%appname%"') DO ( CALL :process "%%~dpi"
)
)
::
:: Now have TARGETS=#hits; $_n=full pathname; $n=directoryname
::
IF %targets%==1 SET mychoice=1&GOTO chosen
::
:: repeat a menu
::
:again
CLS
FOR /l %%i IN (1,1,%targets%) DO (
IF %%i lss 10 (ECHO(%%i%dots%%dots:~0,1%!$%%i!) ELSE (ECHO(%%i%dots%!$%%i!)
)
SET mychoice=
SET /p mychoice="Please choose 1..%targets% : "
IF NOT DEFINED $_%mychoice% GOTO again
:chosen
CALL SET application="%%$_%mychoice%%%%appname%"
ECHO START "" /b %application%
GOTO :EOF
::
:: Parameter is quoted-full-pathname
::
:process
SET /a targets+=1
SET $_%targets%=%~1
SET $=%~1
FOR %%n IN ("%$:~0,-1%") DO SET $%targets%=%%~nxn
GOTO :eof
From what you've said, this should work.
First steps are to set up. Set the number of targets found and the real application name and clear out any variablenames that start "$".
Next step is to specify the directories to use. I simply copied yours, then overrode those settings with settings to suit my machine - you'd need to delete those overriding lines, of course. Note that the names should be set quoted...
Next, scan from each of the directories for the application. Each time an instance is found, call :process passing the full -terminated pathname.
:process increments the count of targets found and sets the variable $_n to the full pathname passed (dequoted) It then sets $ to the dequoted-full-pathname and uses a syntax-trick to have FOR find the application's immediate directory. %$:~0,-1% is the full pathname minus the last character - the \ so the results looks like a filename. That "filename" is then assigned to $n
Once the directory-scans are finished, %targets% will contain the er, number of targets. If that's 1, then we have all we need - we can only select target number 1, so that's chosen for us.
Otherwise, clear the screen and display a number of lines - %targets% to be precise. The format isn...immediatedirname` and an extra dot is added if the number of targets is less than 10 so that a long list will line up nicely.
Then ask for a line number.
At this point, the only $_ variables that exist are $_1..$_%targets% so if $_%mychoice% exists, it must be a valid choice. If it's not defined, then repeat the question...
CALLing SET application="%%$_%mychoice%%%%appname%" first parses the command, then parses it again. After the first parse, the line becomes (if mychoice=3) SET application="%$_3%app.exe" so on the second occasion,application` is properly set to the full fulename of the application - and quoted.
There wasn't much point in my starting the app, since it doesn't exist, so I just echoed it.
Given the extended requirement:
You's probably be starting this from a shortcut. Suppose you run that shortcut minimised and insert after the SETLOCAL
SET "poweruser=%1"
Add, after
IF %targets%==1 SET mychoice=1&GOTO chosen
the line
IF NOT DEFINED poweruser START "%username% - poweruser" "%~dpnx0" poweruser&GOTO :EOF
And change the
GOTO :EOF
just prior to the label :process to
EXIT
So that if it wasn't run in poweruser mode (therefore poweruser is NOT defined) AND %targets% is NOT 1 (I'm presuming it won't be 0) Then this user hasn't been set as a poweruser in the shortcut, but does have more than one target, so the job is restarted in poweruser mode (ie. maximised.)
Note that it doesn't actually matter what the string is after the "~dpnx0" - so long as it's a string. I just used poweruser What it does is tell the batch that it's being run in a normal window, not minimised.
For powerusrs, you could if you like set the shortcut to normal window or maximised AND put a parameter in the command line after the batchname, which will marginally quicken the procedure. Leaving it without parameters and minimised would suit ALL users as it will auto-adjust if more than 1 target is found.
I have tried this. It worked for me.
I answered your question in two steps. In the first one I translated your original code into a more readable one. Here it is:
#ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET "DIR1=%USERPROFILE%\AppData\Local\Application Workspace\Profiles"
SET DIR2=C:\Application\SRW\Profiles
IF NOT EXIST "%DIR1%" (
for /f "delims=" %%A in ('dir %DIR2% /ad /b') do (
SET DIR=%DIR2%\%%~nxA\app.exe
)
) ELSE (
for /f "delims=" %%A in ('dir %DIR1% /ad /b') do (
SET DIR=%DIR1%\%%~nxA\app.exe
)
)
START "" /b "%DIR%"
ENDLOCAL
EXIT
You should check first that previous code is equivalent to your original one.
In the second step I added the profile selection to the previous code:
#ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET "DIR1=%USERPROFILE%\AppData\Local\Application Workspace\Profiles"
SET DIR2=C:\Application\SRW\Profiles
IF NOT EXIST "%DIR1%" (
for /f "delims=" %%A in ('dir %DIR2% /ad /b') do (
SET DIR=%DIR2%\%%~nxA\app.exe
)
) ELSE (
call :SelectProfile
)
START "" /b "%DIR%"
ENDLOCAL
EXIT
:SelectProfile
rem Get a list of folders in User Profile dir
set count=0
for /f "delims=" %%A in ('dir %DIR1% /ad /b') do (
set /A count+=1
SET DIR[!count!]=%DIR1%\%%~nxA
)
rem Initialize the selected profile
set select=1
if %count% equ 1 goto endSelection
rem Show the profiles menu
cls
echo Available profiles:
echo/
for /L %%i in (1,1,%count%) do echo %%i- !DIR[%%i]!
echo/
rem Let's the user to select the profile
:select
set select=1
set /P "select=Enter number of desired profile [first one]: "
if not defined DIR[!select!] goto select
:endSelection
SET DIR=!DIR[%select%]!\app.exe
exit /B
Please note that previous code fail if the user enter spaces in the answer. This detail may be fixed, if needed.

issues with enabledelayedexpansion for file renaming batch script

i am writing a batch script monotonic file renamer. basically, it makes the titles of all the files 1 2 3 4 .... and so on. i have since expanded it to be able to handle files of different types (txt, doc, flv, etc) but not everything is working out.
my main concern is i have broken the delayed expansion calls i was making before. now using !var1! is never expanded, or never recognized as a variable.
here is a verbosely commented version of my script
::a monotonic file renamer
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET tempfile=temp.txt
SET exttemp=exttemp.txt
if [%1] == [] goto usage
::make sure your dont overwrite something useful
if EXIST %tempfile% (
ECHO Temp file already exists, are you sure you want to delete?
del /P %tempfile%
)
if EXIST %exttemp% (
ECHO EXT Temp file already exists, are you sure you want to delete?
del /P %exttemp%
)
::initialize
SET /a counter=0
SET type=
SET /a ender=%1
::write filenames to tempfile
DIR /B /ON > %tempfile%
::read lines one by one
for /f "usebackq delims=" %%a in (%tempfile%) do (
REM make sure we do not rename any of the working files
if NOT "%%a"=="renamer.bat" (
if NOT "%%a"=="temp.txt" (
if NOT "%%a"=="exttostr.bat" (
SET /a counter+=1
REM get file extension
exttostr %%a > %exttemp%
SET /P type= < %exttemp%
REM housekeeping
del /F %exttemp%
REM rename
ren %%a !counter!.!type!
ECHO Renamed "%%a" to "!counter!.!type!"
)))
REM exit when we have run enough
if "!counter!"=="!ender!" goto exit
)
goto exit
:usage
echo Usage: renamer NUMFILES
:exit
::final housekeeping
DEL temp.txt
the idea is i drop my two files, renamer.bat(this file) and exttostr.bat(helper to get the file extension) into the folder and run it, it will rename files sorted alphabetically from 1 to how ever many files i specify.
when i run the code, it never uses the variables marked for delayed expansion appropriately, always leaving them as "!varname!", so it renames the first file "!counter!.!type!" and throws errors for the rest because there is already a file in the directory with that name.
this brings me to a secondary issue. sorting the dir list alphabetically results in a poor handling of numbered files. for example the list:
"1 7 15 75 120"
is sorted:
"1 120 15 7 75"
i have not been able to find a way around this yet, only that it is indeed the intended result of the dir sort. the only workaround i have is padding numbers with enough zeroes in the front.
thanks in advance for any insight!
everything is sorted but the second problem. i think i have not spoken well. i have this issue when i take IN the directory file names, not when writing out. so they already need to be padded. i has hoping there was some other way to read the directory and have it be sorted appropriately.
the most promising thing i have found is here: http://www.dostips.com/DtCodeBatchFiles.php#Batch.SortTextWithNumbers
#ECHO OFF
if "%~1"=="/?" (
echo.Sorts text by handling first number in line as number not text
echo.
echo.%~n0 [n]
echo.
echo. n Specifies the character number, n, to
echo. begin each comparison. 3 indicates that
echo. each comparison should begin at the 3rd
echo. character in each line. Lines with fewer
echo. than n characters collate before other lines.
echo. By default comparisons start at the first
echo. character in each line.
echo.
echo.Description:
echo. 'abc10def3' is bigger than 'abc9def4' because
echo. first number in first string is 10
echo. first number in second string is 9
echo. whereas normal text compare returns
echo. 'abc10def3' smaller than 'abc9def4'
echo.
echo.Example:
echo. To sort a directory pipe the output of the dir
echo. command into %~n0 like this:
echo. dir /b^|%~n0
echo.
echo.Source: http://www.dostips.com
goto:EOF
)
if "%~1" NEQ "~" (
for /f "tokens=1,* delims=," %%a in ('"%~f0 ~ %*|sort"') do echo.%%b
goto:EOF
)
SETLOCAL ENABLEDELAYEDEXPANSION
set /a n=%~2+0
for /f "tokens=1,* delims=]" %%A in ('"find /n /v """') do (
set f=,%%B
(
set f0=!f:~0,%n%!
set f0=!f0:~1!
rem call call set f=,%%%%f:*%%f0%%=%%%%
set f=,!f:~%n%!
)
for /f "delims=1234567890" %%b in ("!f!") do (
set f1=%%b
set f1=!f1:~1!
call set f=0%%f:*%%b=%%
)
for /f "delims=abcdefghijklmnopqrstuwwxyzABCDEFGHIJKLMNOPQRSTUWWXYZ~`##$*_-+=:;',.?/\ " %%b in ("!f!") do (
set f2=00000000000000000000%%b
set f2=!f2:~-20!
call set f=%%f:*%%b=%%
)
echo.!f1!!f2!!f!,%%B
rem echo.-!f0!*!f1!*!f2!*!f!*%%a>&2
)
this code can sort the filenames with one number in them (i.e. video100.mov is fine, video100video10.mov would break it)
the issue i have is i think adding a call to this helper fn will break it again, so i will be trying to include this in my modified renamer.bat now. any help is appreciated.
Probably the batch for extracting the extension reset the local environment.
But, you don't need it. You may extract the extension with the ~x option. Something similar to this ....
:monotonicrename
set /a counter = 0
for %%a in (%1\*.*) do (
if exist %%~fa (
set /a counter += 1
echo ren %%~fa !counter!%%~xa
)
)
goto :eof
to include leading zeroes in the counter, so that the directory sorts correctly, replace the previous rename command with three lines
set zcounter=0000!counter!
set zcounter=!zcounter:~-4!
echo ren %%~fa !counter!%%~xa
So putting all pieces together, add the monotonicrename function you just created in the batch file that can be as simpler as...
#echo off
setlocal enabledelayedexpansion
call :monotonicrename %1
goto :eof
:monotonicrename
set /a counter = 0
for %%a in (%1\*.*) do (
if exist %%~fa (
set /a counter += 1
set zcounter=0000!counter!
set zcounter=!zcounter:~-4!
echo ren %%~fa !zcounter!%%~xa
)
)
goto :eof
I didn't experience any issues with delayed expansion, everything worked fine for me (except, of course, for the fact that I didn't have the exttostr.bat helper script.)
Anyway, there are several things that could be improved about your script:
You don't need to store the result of DIR into a file to read it afterwards. You can read the output directly in the FOR loop.
You don't need the helper batch script. The extension can be extracted from %%a by using the ~x modifier with the loop variable: %%~xa. You can read more about modifiers by issuing HELP FOR from the command prompt.
The renamer batch file's own name can be referenced in the script as %0. You can apply the ~n modifier where you only need to use the name without the extension. The combined modifier of ~nx will give you the name with the extension.
So, here's how your script might look like with the above issues addressed:
::a monotonic file renamer
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
IF [%1] == [] GOTO usage
::initialize
SET /A counter=0
SET type=
SET /A ender=%1
::read lines one by one
FOR /F "usebackq delims=" %%a IN (`DIR /B /ON`) DO (
REM make sure we do not rename any of the working files
IF NOT "%%~a"=="%~nx0" (
SET /A counter+=1
RENAME "%%~a" "!counter!%%~xa"
ECHO Renamed "%%~a" to "!counter!%%~xa"
)
REM exit when we have run enough
IF "!counter!"=="!ender!" GOTO :EOF
)
GOTO :EOF
:usage
ECHO Usage: %~n0 NUMFILES
As for your secondary issue, it can be easily resolved like this:
Use something like 100000 as counter's initial value. (Use however many 0s you like, but possibly no more than nine.) Add the same value to ender as well.
When renaming files, instead of !counter! use the expression that removes the first character (the 1): !counter:~1! (in fact, this is not about removal, but about extracting a substring starting from the offset of 1, learn more about it with the HELP SET command).
Here's the modified version of the above script:
::a monotonic file renamer
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
IF [%1] == [] GOTO usage
::initialize
SET /A counter=1000
SET type=
SET /A ender=%1
SET /A ender+=counter
::read lines one by one
FOR /F "usebackq delims=" %%a IN (`DIR /B /ON`) DO (
REM make sure we do not rename any of the working files
IF NOT "%%~a"=="%~nx0" (
SET /A counter+=1
RENAME "%%~a" "!counter:~1!%%~xa"
ECHO Renamed "%%~a" to "!counter:~1!%%~xa"
)
REM exit when we have run enough
IF "!counter!"=="!ender!" GOTO :EOF
)
GOTO :EOF
:usage
ECHO Usage: renamer NUMFILES
You can also see that I made some other enhancements, like making sure the file name is enclosed in double quotes, and using GOTO :EOF instead of GOTO exit (:EOF is a special pre-defined label that points at the end of the batch script so you don't need to define your own).

Resources