Delayed Expansion causing my script window to close but not fail - windows

I have a set of variables that represent directory names: folder_1, folder_2,... folder_n.
In my code I am trying to set the variable FOLDER to the correct directory name based on the user input selection after validating the value with a conditional if statement. However, as my code is, the FOLDER variable is assigned with double quote which causes the DESTINATION variable to end up looking something like this: C:\User\stackoverflow\"chosen folder"
When attempting to use this variable as the destination for a move command it fails.
#echo off
setlocal EnableDelayedExpansion
REM totally nothing import happening up here
set /p selection=""
if %selection% GEQ 0 if %selection% LEQ %a% (
echo you selected folder !folder_%selection%!
set FOLDER="!folder_%selection%!"
set DESTINATION=!PATH!\Bermuda Triangle\!FOLDER!
set /a pass=1
)
REM nothing here either
move /-y C:\wherever\this\file\is DESTINATION
pause
Okay, that's fine, I'll just lose the quotes in line 10.
set FOLDER=!folder_%selection%!
Running my script with these changes "apparently" fixes the syntax issue for the move command, because it gets moved to where it is supposed to; however, the pause command at the end of my script no longer works and the window closes. So while it works I can't check my echo feedback calls to make sure everything is functioning as intended.
Can anyone help me understand what is going on here?

Related

How to Increment a number in the name of a file everytime the Batch file is run

I am using CommandCam, a software which takes a photo from specified webcam everytime it is run. I am using it to take a photo whenever my computer starts up, but what the software does is it overwrites the previous image taken with the same name. So I created a .bat file that checks if image.bmp exists, save the new image file as image_1.bmp. However it doesn't seem to work.
I have tried to use GOTO but it doesn't work.
#echo off
if exist image.bmp (goto existing) else (goto nonexisting)
:nonexisting
set /A imgnum = 0
.\CommandCam.exe
exit /B
:existing
set /A imgnum+=1
.\CommandCam.exe /filename image_%imgnum%.bmp
exit /B
The first time the .bat is run, it works as expected. A image.bmp file is created. And next time the .bat is run, it works too, A image_1.bmp is created. But the third and so on time the .bat is run, it doesn't work and just overwrites image_1.bmp . Any Help?
The batch file should check image.bmp, image1.bmp, image2.bmp, etc. until it finds a non-existing file. Like this:
#echo off
setlocal
set imgnum=
if not exist image.bmp goto nonexisting
set imgnum=0
:existing
set /A imgnum += 1
if exist image%imgnum%.bmp goto existing
:nonexisting
echo >image%imgnum%.bmp
endlocal
The setlocal/endlocal make sure the environment isn't left with imagnum set to some value. I just used echo to create the file for testing.

How to Increment a FileName in batch? (Part2)

I already tried the answer given by Mofi in my last question regarding this topic. But I changed the base name and it does not seem to work by now. If you want to see the previous question:How do I increment a filename in batch? What is wrong with this new code? It does not make a new file it just overwrites the previous made file.
:MainProcessNew
cd /D "%USERPROFILE%\Desktop"
for /F %%G in (*.json) do (
set "FileName=%%G"
set "BaseName=Device"
set "FileNumber=0"
)
:FileNameLoop
set /A FileNumber+=1
if exist "%BaseName%%FileNumber%.json" (
goto FileNameLoop
)
echo.>"%USERPROFILE%\Desktop\%BaseName%%FileNumber%.json"
I'm quite sure the batch code in question is not complete or reduced to a script code not really suitable to reproduce the problem because with Device.json existing in folder Desktop and no other Device*.json file existing, the empty line is first written to Device1.json. A file with name Device.json is never overwritten by the batch code in question because variable FileNumber has always at least the value 1.
Well, the FOR option /F is most likely wrong here as I suppose the FOR loop should search for *.json files as done without /F. Using a wildcard pattern like *.json together with option/F results in error message:
The system cannot find the file *.json.
Run in a command prompt window for /? or help for for help on syntax of this command.
It is completely unclear what is the purpose of the FOR loop because the FileName variable is not used at all. This variable should perhaps hold the name of last found *.json if there was any *.json file found at all. But that also does not make sense if not further used anywhere.
It is also unclear why BaseName and FileNumber are defined inside the loop and not outside.
In the complete batch code the label FileNameLoop is perhaps the beginning of a subroutine. But in the reduced batch code in question there is no call :FileNameLoop "%%G" which I would expect in this case.
So the question is hard to answer as it is unclear what is really the problem with posted batch code.
:MainProcessNew
cd /D "%USERPROFILE%\Desktop"
rem Useless FOR loop without /F commented out.
rem for %%G in (*.json) do set "FileName=%%G"
set "BaseName=Device"
set "FileNumber="
rem Skip searching for files with a file number
rem after Device if there is no Device.json file.
if not exist "%BaseName%.json" goto CreateFile
rem Otherwise keep Device.json as is and search for Device1.json,
rem Device2.json, ... until a Device*.json file with current number
rem is not found and use this number for next Device*.json file.
set "FileNumber=0"
:FileNameLoop
set /A FileNumber+=1
if exist "%BaseName%%FileNumber%.json" goto FileNameLoop
:CreateFile
rem Note: FileNumber is replaced in the line below by an empty
rem string if there is no Device.json in desktop folder.
echo.>"%USERPROFILE%\Desktop\%BaseName%%FileNumber%.json"
Hint: For debugging a batch file
comment out or remove all #echo off or echo off in the batch file or change off to on,
open a command prompt window,
enter "Path to batch file\BachFileName.bat" and press RETURN.
Now it can be seen in the command prompt window each line executed by Windows command processor after preprocessing each line and each command block which means after replacing all %VariableName% by the current value of the variable in current line or entire command block.
And error messages can be also seen as the command prompt window remains open after processing batch file stopped, except it contains the command exit without option /B which always terminates the current command process.

Check Batch File Location before Executing

I have wrote a batch file to Backup User Profile, but I would like to make it idiot proof.
A few times I have actually left the batch file on my desktop and run it on my own profile and as the batch file contains a robocopy command to copy the Desktop folder, it creates an infinite loop of duplicate folder inside one and other, which are difficult to get rid off due to the 256 character limit.
I am trying to get the batch file to check its own location is not within the user profile before executing.
I was messing around last night with the IF command and this is what i came up with:
#ECHO off
SET /P %UserName%=Enter Username:
IF "%~dp0"=="C:\Users\%UserName%\NUL" (
GOTO ERROR
) ELSE (
GOTO PROFILEBACKUP
)
:PROFILEBACKUP
REM To Be replaced by Profile Backup Script
ECHO Correct Location
:ERROR
ECHO Move ProfileBackup.CMD to another location
But the only output I get is:
Enter Username: smithsl
Correct Location
Move ProfileBackup.CMD to another location
Any help would be appreciated
Here is a suggestion for a batch code checking if location of batch file is somewhere in user's profile directory tree.
#echo off
setlocal EnableDelayedExpansion
set "BatchPath=%~dp0"
:EnterName
rem Define as default name of current user.
set "NameOfUser=%UserName%"
set /P "NameOfUser=Enter user name (default: %NameOfUser%): "
rem Remove double quotes from entered string.
set "NameOfUser=!NameOfUser:"=!"
rem Were something different than just double quotes entered?
if "%NameOfUser%"=="" goto EnterName
rem Remove also angle brackets and exclamation marks.
set "NameOfUser=!NameOfUser:<=!"
if "%NameOfUser%"=="" goto EnterName
set "NameOfUser=!NameOfUser:>=!"
if "%NameOfUser%"=="" goto EnterName
set "NameOfUser=%NameOfUser:!=%"
if "%NameOfUser%"=="" goto EnterName
rem Starts the path of the batch file with C:\Users\entered user name?
if not "!BatchPath:C:\Users\%NameOfUser%=!" == "%BatchPath%" goto LocationError
rem To be replaced by profile backup script
echo Correct Location
endlocal
goto :EOF
:LocationError
echo Move %~nx0 to another location.
endlocal
goto :EOF
Although some tests are made for checking invalid user input, it is still not idiot proof as this is nearly impossible when a user can enter something.
The mistake you made was assigning entered value to %UserName% which means if your user account name is for example Scott, the entered string is assigned to an environment variable with name Scott and not to environment variable with name UserName. It is of course no good idea to overwrite the predefined UserName environment variable value by a batch script.
Open a command prompt window, execute there set /? and read the help printed into the console window to understand the string replacements used in this batch code using additionally delayed environment variable expansion.

bat script for loop works on CL but does not work when double clicked

I have a batch file and inside the batch file, it looks in a particular directory and saves to a variable, the name of the first directory/file. Here is roughly what I am doing:
FOR /d %%F IN (%INSTALL_DIR%\dir\*) DO (
set NAME=%%~xnF
set NAME_DIR=%INSTALL_DIR%\dir\%NAME%
goto :break
)
When I run this from the command line, it works perfectly and NAME_DIR gets the correct value. However, when I double-click on the file, the NAME variable is blank. NANE_DIR is thus set to %INSTALL_DIR%\dir. Why does this happen and what can I do to fix it?
For more clarification, from the command line, this is what NAME and NAME_DIR equal when echoed:
NAME: dir1.3.8
NAME_DIR: D:\root\path\to\dir\dir1.3.8
This is what is echoed when double clicked:
NAME:
NAME_DIR: D:\root\path\to\dir
What RaymondChen told me in the comments worked! I needed to use delayed expansion. The reason for this is that windows executes the for loop as one single instruction and so it fills in all the variables at once before executing the for loop. Since I never had NAME set before the for loop, it would just evaluate to nothing. Therefore NAME_DIR gets an empty string in place of %NAME%. I noticed that when I first ran the script on the command line, it did not work. I ran it again and it worked. I kept running it and it worked every time. Thats because after running the for loop once, the variable is saved and never changed. So the next time I run the code, the variable is not blank anymore, it has the correct value. Now when double clicking, the NAME variable is null at the beginning. After the for loop executes, the value of NAME is updated. Once the script executes and the cmd windows exits, the new value of the variable is lost and it starts all over again. Here is the new code with delayed expansion:
setlocal ENABLEDELAYEDEXPANSION
FOR /d %%F IN (%INSTALL_DIR%\dir\*) DO (
set NAME=%%~xnF
set NAME_DIR=%INSTALL_DIR%\dir\!NAME!
goto :break
)
I added in the setlocal statement and changed %NAME% to !NAME!

Loop over folder string and parse out last folder name

I need to grab the folder name of a currently executing batch file. I have been trying to loop over the current directory using the following syntax (which is wrong at present):
set mydir = %~p0
for /F "delims=\" %i IN (%mydir%) DO #echo %i
Couple of issues in that I cannot seem to pass the 'mydir' variable value in as the search string. It only seems to work if I pass in commands; I have the syntax wrong and cannot work out why.
My thinking was to loop over the folder string with a '\' delimiter but this is causing problems too. If I set a variable on each loop then the last value set will be the current folder name. For example, given the following path:
C:\Folder1\Folder2\Folder3\Archive.bat
I would expect to parse out the value 'Folder3'.
I need to parse that value out as its name will be part of another folder I am going to create further down in the batch file.
Many thanks if anyone can help. I may be barking up the wrong tree completely so any other approaches would be greatly received also.
After struggling with some of these suggestions, I found an successfully used the following 1 liner (in windows 2008)
for %%a in (!FullPath!) do set LastFolder=%%~nxa
You were pretty close to it :) This should work:
#echo OFF
set mydir="%~p0"
SET mydir=%mydir:\=;%
for /F "tokens=* delims=;" %%i IN (%mydir%) DO call :LAST_FOLDER %%i
goto :EOF
:LAST_FOLDER
if "%1"=="" (
#echo %LAST%
goto :EOF
)
set LAST=%1
SHIFT
goto :LAST_FOLDER
For some reason the for command doesn't like '\' as a delimiter, so I converted all '\' to ';' first (SET mydir=%mydir:\=;%)
I found this old thread when I was looking to find the last segment of the current directory.
The previous writers answers lead me to the following:
FOR /D %%I IN ("%CD%") DO SET _LAST_SEGMENT_=%%~nxI
ECHO Last segment = "%_LAST_SEGMENT_%"
As previous have explained, don't forget to put quotes around any paths create with %_LAST_SEGMENT_% (just as I did with %CD% in my example).
Hope this helps someone...
This question's a little old, but I've looked for a solution more than once so here's a completely new take on it that I've just put together.
The trick is that we take the desired path, back up one level to create a folder mask for substitution and then replace the folder mask with nothing.
To test it, simple copy and paste into a command script (.cmd) in any directory, then run it. It will spit out only the deepest directory you're currently in.
Notes:
Replace %~dp0 with whatever path you like (as it is, it will return the deepest folder the batch file is run from. This is not the same as %cd%.)
When specifying the 'pathtofind' variable ensure there are no quotes e.g. c:\some path and not "c:\some path".
The original idea for folder masking is mine
Spaces in the path are no problem
Folder depth is not a problem
It was made possible by the genius of this batch scripting tip http://www.dostips.com/DtCodeBatchFiles.php#Batch.FindAndReplace
Hope this helps someone else.
#echo off
set pathtofind=%~dp0
if not exist %pathtofind% echo Path does not exist&pause>nul&goto :eof
cd /d %pathtofind%
set path1=%cd%
cd ..
set path2=%cd%
call set "path3=%%path1:%path2%\=%%"
echo %path3%
pause>nul
3 lines of script gets the result...
Found 2 additional ways to accomplish the goal, and unlike the other answers to this question, it requires no batch "functions", no delayed expansion, and also does not have the limitation that Tim Peel's answer has with directory deepness :
#echo off
SET CDIR=%~p0
SET CDIR=%CDIR:~1,-1%
SET CDIR=%CDIR:\=,%
SET CDIR=%CDIR: =#%
FOR %%a IN (%CDIR%) DO SET "CNAME=%%a"
ECHO Current directory path: %CDIR%
SET CNAME=%CNAME:#= %
ECHO Current directory name: %CNAME%
pause
REVISION: after my new revsion, here is an example output:
Current directory path: Documents#and#Settings,username,.sqldeveloper,tmp,my_folder,MY.again
Current directory name: MY.again
Press any key to continue . . .
This means that the script doesn't handle '#' or ',' in a folder name but can be adjusted to do so.
ADDENDUM: After asking someone in the dostips forum, found an even easier way to do it:
#echo off
SET "CDIR=%~dp0"
:: for loop requires removing trailing backslash from %~dp0 output
SET "CDIR=%CDIR:~0,-1%"
FOR %%i IN ("%CDIR%") DO SET "PARENTFOLDERNAME=%%~nxi"
ECHO Parent folder: %PARENTFOLDERNAME%
ECHO Full path: %~dp0
pause>nul
To return to the original poster's issue:
For example, given the following path:
C:\Folder1\Folder2\Folder3\Archive.bat
I would expect to parse out the value 'Folder3'.
The simple solution for that is:
for /D %%I in ("C:\Folder1\Folder2\Folder3\Archive.bat\..") do echo parentdir=%%~nxI
will give 'Folder3'. The file/path does not need to exist. Of course, .... for the parent's parent dir, or ...... for the one above that (and so on) work too.
Slight alteration for if any of the folders have spaces in their names - replace space to ':' before and after operation:
set mydir="%~p0"
set mydir=%mydir:\=;%
set mydir=%mydir: =:%
for /F "tokens=* delims=;" %%i IN (%mydir%) DO call :LAST_FOLDER %%i
goto :EOF
:LAST_FOLDER
if "%1"=="" (
set LAST=%LAST::= %
goto :EOF
)
set LAST=%1
SHIFT
goto :LAST_FOLDER
Sheesh guys, what a mess. This is pretty easy, and it's faster to do this in memory without CD.
This gets the last two directories of a path. Modify it as required to get the last tokens of any line. My original code I based this on has more complexity for my own purposes.
Fyi, this probably doesn't allow paths with exclamation marks since I'm using enabledelayedexpansion, but that could be fixed.
It also won't work on a plain drive root. This could be averted in a number of ways. Check what the input path ends with, or a counter, or modifying the token and check behaviour, etc.
#echo off&setlocal enableextensions,enabledelayedexpansion
call :l_truncpath "C:\Windows\temp"
----------
:l_truncpath
set "_pathtail=%~1"
:l_truncpathloop
for /f "delims=\ tokens=1*" %%x in ("!_pathtail!") do (
if "%%y"=="" (
set "_result=!_path!\!_pathtail!"
echo:!_result!
exit/b
)
set "_path=%%x"
set "_pathtail=%%y"
)
goto l_truncpathloop
I modified answer given by #Jonathan, since it did not work for me in a batch file, but this below does work, and also supports folders with spaces in it.:
for %%a in ("%CD%") do set LastFolder=%%~nxa
echo %LastFolder%
This takes the current directory and echoes the last, deepest folder, as in below example, if the folder is this:
C:\Users\SuperPDX\OneDrive\Desktop Environment\
The batch code echoes this: Desktop Environment
In batch files in the FOR command you'll need to prepend %whatever with an extra % (e.g. %%whatever).
'echo %~p0' will print the currently directory of the batch file.
This is what we had in the end (little bit more crude and can only go so deep :)
#echo off
for /f "tokens=1-10 delims=\" %%A in ('echo %~p0') do (
if NOT .%%A==. set new=%%A
if NOT .%%B==. set new=%%B
if NOT .%%C==. set new=%%C
if NOT .%%D==. set new=%%D
if NOT .%%E==. set new=%%E
if NOT .%%F==. set new=%%F
if NOT .%%G==. set new=%%G
if NOT .%%H==. set new=%%H
if NOT .%%I==. set new=%%I
if NOT .%%J==. set new=%%J
)
#echo %new%
I don't know if it's the version of windows I'm on (win2k3), but the FOR loop isn't giving me anything useful for trying to iterate through a single string.
According to my observation (and the FOR /? info) you get one iteration for each line of input to FOR, and there is no way to change this to iterate within a line. You can break into multiple tokens for a given line, but it is only one invocation of the FOR loop body.
I do think the CALL :LABEL approach in these answers does a great job. Something I didn't know until looking at this was that ";" and "," are both recognized as argument separators. So once you replace backslashes with semicolons, you can call your label and iterate through with SHIFT.
So working off of what is posted by others here, I have the below solution. Instead of grabbing the last folder name, I actually wanted to find everything up until some known directory name.. this is what is implemented below.
#echo off
if "%1"=="" goto :USAGE
set FULLPATH=%~f1
set STOPDIR=%2
set PATHROOT=
:: Replace backslashes with semicolons
set FULLPATH=%FULLPATH:\=;%
:: Iterate through path (the semicolons cause each dir name to be a new argument)
call :LOOP %FULLPATH%
goto :EOF
:LOOP
::Exit loop if reached the end of the path, or the stop dir
if "%1"=="" (goto :EOF)
if "%1"=="%STOPDIR%" (goto :EOF)
::If this is the first segment of the path, set value directly. Else append.
if not defined PATHROOT (set PATHROOT=%1) else (set PATHROOT=%PATHROOT%\%1)
::shift the arguments - the next path segment becomes %i
SHIFT
goto :LOOP
:USAGE
echo Usage:
echo %~0 ^<full path to parse^> ^<dir name to stop at^>
echo E.g. for a command:
echo %~0 c:\root1\child1\child2 child2
echo The value of c:\root1\child1 would be assigned to env variable PATHROOT
Unfortunatelly, this is working great only when put on some depth but have problems with being on the very top of the mountain... Putting this program into "C:\Windows" e.g. will result with... "C:\Windows", not expected "Windows". Still great job, and still damage can be repaired. My approach:
#echo off
set pathtofind=%~dp0
if not exist %pathtofind% echo Path does not exist&pause>nul&goto :eof
cd /d %pathtofind%
set path1=%cd%
cd ..
set path2=%cd%
set path4=%~dp1
call set "path3=%%path1:%path2%\=%%"
call set "path5=%%path3:%path4%*\=%%"
echo %path5%
pause>nul
And it's working just fine for me now, thanks for the idea, I was looking for something like that for some time.

Resources