Windows batch scripting: compare two files' created dates - windows

I would like to fork my Windows batch script based on a comparison of the created dates of two files, and I'm not sure where to begin. I feel like there must be a way. Any ideas?
UPDATE:
Tried the solution in PA's answer. I copied the code snippet verbatim to the end of my current script. Then, I added this early in the script:
IF EXIST "%PROGRAMFILES(X86)%" CALL :getCreationDate "%PROGRAMFILES(X86)%\oracle\jinitiator 1.3.1.28\lib\security\certdb.txt"
which gives me an error when I execute:
Invalid alias verb.

You need to put a caret before the equals sign to escape it (cmd.exe is sooo wonderful). I've verified this works:
setlocal enableextensions enabledelayedexpansion
call :getCreationDate "C:\Windows\Notepad.exe"
echo Creation Date is: %creationdate%
endlocal
goto :EOF
:getCreationDate
set FILE=%~f1
set FILE=%FILE:\=\\%
for /F "skip=1 tokens=* usebackq" %%A in (`wmic datafile where name^="%FILE%" get creationdate`) do (
set creationdate=%%A
)
goto :EOF

In a bat you can get the creation date of a file with WMIC DATAFILE command, using the GET CREATIONDATE verb.
You need to capture the output of the command into a variable, see HELP FOR and HELP SET.
You can use :label and GOTO :eof to create a function that puts together this functionality.
Notice that for WMIC DATAFILE, the WHERE NAME= clause requires a fully specified filename. See HELP CALL and the %~f modifier.
Notice also that WMIC DATAFILE WHERE NAME= requires doubling the backslashes in the filename. See HELP SET and the % : = % syntax for replacing single backslashes with double backslashes.
something like this.....
:getCreationDate
set FILE=%~f1
set FILE=%FILE:\=\\%
FOR /F "skip=1 tokens=* usebackq" %%A IN (`wmic datafile where name="%FILE%" get creationdate`) DO (
SET CREATIONDATE=%%A
)
goto :eof
You will need to use CALL :label for invoking it.
CALL :getCreationDate myfile.txt
You'll need to extract the part of the datetime you are interested in compating. See HELP SET using the ~ modifier.
Finally, you'll need to compare the returned datefiles. See HELP IF.

try this:
wmic datafile where name='c:\\users\\ovadia\\test\\addx.txt' get 'LAST MODIFIED' > dateofNEWadd.txt
wmic datafile where name='c:\\users\\ovadia\\test\\prevaddx.txt' get 'LAST MODIFIED' > dateofOLDadd.txt
fc /LB1 dateofNEWadd.txt dateofOLDadd.txt
if errorlevel 1 echo "fc err not 0"
del dateof*
attributes for the 'get' may be...
Access Rights,
Caption,
Class Name,
Compressed,
Compression Method,
Computer System Class Name,
Computer System Name,
Creation Date,
Current File Open Count,
Description,
Drive,
Eight Dot Three File Name,
Encrypted,
Encryption Method,
File Extension,
File Name,
File System Class Name,
File System Name,
File Type,
Hidden,
Install Date,
Last Accessed,
Last Modified,
Manufacturer,
Name,
Path,
Readable,
Should Be Archived,
Size,
Status,
System File,
Version,
Writeable

Related

Windows batch command to create backup folder and replace folder

I need to backup an existing folder with date-time stamp and replace it (delete and recreate) with new content inside the folder.
Does anyone have a script to do this?
I tried the following code, where %ApplicationDeploymentFolderPath% = \\servername\foldername
IF EXIST %ApplicationDeploymentFolderPath%\Release (
REM Get current date time
#echo off
For /f "tokens=1-3 delims=/ " %%a in ('date /t') do (set mydate=%%c_%%b_%%a)
For /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a%%b)
set backup_folder=%mydate%_%mytime%
MD %ApplicationDeploymentFolderPath%\%backup_folder%
REM Copy current folder to backup folder
Copy %ApplicationDeploymentFolderPath%\Release %ApplicationDeploymentFolderPath%\%backup_folder%
REM Delete Existing Release folder
RD %ApplicationDeploymentFolderPath%\Release /S /Q
)
MD %ApplicationDeploymentFolderPath%\Release
The command date with parameter /T outputs the current date in format defined by configured country for current user account. Exactly the same date string can be accessed by referencing dynamic environment variable DATE for example with %DATE%.
The command time with parameter /T outputs the current time in format defined by configured country for current user account. Exactly the same time string can be accessed by referencing dynamic environment variable TIME for example with %TIME%.
What happens on execution of this command line?
For /f "tokens=1-3 delims=/ " %%a in ('date /t') do (set mydate=%%c_%%b_%%a)
for respectively cmd.exe processing the batch file starts in background one more command process using %ComSpec% /c with the command line between '. So executed in background is following with Windows installed in C:\Windows:
C:\Windows\System32\cmd.exe /c date /t
The output of command date to handle STDOUT of this command process in background is captured by FOR respectively Windows command processor instance executing the batch file.
The captured line is split up into three substrings using / as string delimiter assigned to the loop variables a, b and c which are concatenated together in reverse order with underscore as delimiter.
This task can be done much faster by replacing 'date /t' by "%DATE%". In this case FOR processes the date string expanded by already running cmd.exe on parsing this command line before executing FOR. So there is no starting of one more cmd.exe in background and capturing its output just to process the same date string which makes batch file execution a bit faster.
The same is true for 'time /t' which can be replaced by "%TIME%".
But the two FOR loops could be completely optimized away by using string substitution as described for example by answer on What does %date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2% mean? and region dependent date and time format is well known for example by running in a command prompt window:
echo %DATE% %TIME%
This command outputs on my computer with German date/time format according to configured country:
24.07.2019 20:15:29,90
It can be seen on this output that the original code would not work on my Windows computer with my account because of date string contains . and not / and time string contains a comma.
So better would be using a region independent solution as explained very detailed in answer on Why does %date% produce a different result in batch file executed as scheduled task? The disadvantage is that execution of wmic.exe takes much longer than cmd.exe needs to reformat date and time string to yyyy_MM_dd_HHmm. However, the batch file is executed most likely not very often per day, and so it does not really matter if execution to get date/time in this format takes some milliseconds or about one second.
Copying the entire folder is not really necessary in this case. It should be enough to rename it with:
ren "%ApplicationDeploymentFolderPath%\release" "%backup_folder%"
The command move could be also used if command ren cannot be used for unknown reasons.
However, the main problem is missing knowledge about how and when to use delayed expansion. Open a command prompt, run set /? and read the output help explaining on an IF and a FOR example delayed environment variable expansion.
The issue here is that backup_folder is not defined on executing the command lines referencing it with %backup_folder% because of all occurrences of %variable% are replaced by Windows command processor already on parsing entire command block starting here with ( on IF condition at top by current value of the referenced environment variable before executing the command IF.
So executed on existing release folder is:
set backup_folder=
MD \\servername\foldername\
REM Copy current folder to backup folder
Copy \\servername\foldername\Release \\servername\foldername\
REM Delete Existing Release folder
RD \\servername\foldername\Release /S /Q
This can be seen by debugging the batch file.
See also: How does the Windows Command Interpreter (CMD.EXE) parse scripts?
The solution is here avoiding the command block by changing the first IF condition.
Fast region dependent solution:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "ApplicationDeploymentFolderPath=\\servername\foldername"
if not exist "%ApplicationDeploymentFolderPath%\Release\" goto CreateFolder
ren "%ApplicationDeploymentFolderPath%\Release" "%DATE:~-4%_%DATE:~-7,2%_%DATE:~-10,2%_%TIME:~0,2%%TIME:~3,2%"
:CreateFolder
md "%ApplicationDeploymentFolderPath%\Release"
endlocal
Slower region independent solution:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "ApplicationDeploymentFolderPath=\\servername\foldername"
if not exist "%ApplicationDeploymentFolderPath%\Release\" goto CreateFolder
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "BackupDateTime=%%I"
set "BackupDateTime=%BackupDateTime:~0,4%_%BackupDateTime:~4,2%_%BackupDateTime:~6,2%_%BackupDateTime:~8,4%"
ren "%ApplicationDeploymentFolderPath%\Release" "%BackupDateTime%"
:CreateFolder
md "%ApplicationDeploymentFolderPath%\Release"
endlocal
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
endlocal /?
for /?
goto /?
if /?
md /?
ren /?
set /?
setlocal /?
wmic /?
wmic os /?
wmic os get /?
wmic os get localdatetime /?

Windows Batch file - strip leading characters

I have a batch file which copies some local files up to a google storage area using the gsutil tool. The gsutil tool produces a nice log file showing the details of the files that were uploaded and if it was OK or not.
Source,Destination,Start,End,Md5,UploadId,Source Size,Bytes Transferred,Result,Description
file://C:\TEMP\file_1.xlsx,gs://app1/backups/file_1.xlsx,2018-12-04T15:25:48.428000Z,2018-12-04T15:25:48.804000Z,CPHHZfdlt6AePAPz6JO2KQ==,,18753,18753,OK,
file://C:\TEMP\file_2.xlsx,gs://app1/backups/file_2.xlsx,2018-12-04T15:25:48.428000Z,2018-12-04T15:25:48.813000Z,aTKCOQSPVwDycM9+NGO28Q==,,18753,18753,OK,
What I would like to do is to
check the status result in column 8 (OK or FAIL)
If the status is OK then move the source file to another folder (so that it is not uploaded again).
The problem is that the source filename is appended with "file://" which I can't seem to remove, example
file://C:\TEMP\file_1.xlsx
needs to be changed into this
C:\TEMP\file_1.xlsx
I am using a for /f loop and I am not sure if the manipulation of the variables %%A is different within a for /f loop.
#echo off
rem copy the gsutil log file into a temp file and remove the header row using the 'more' command.
more +1 raw_results.log > .\upload_results.log
rem get the source file name (column 1) and the upload result (OK) from column 8
for /f "tokens=1,8 delims=," %%A in (.\upload_results.log) do (
echo The source file is %%A , the upload status was %%B
set line=%%A
set line=!line:file://:=! >> output2.txt echo !line!
echo !line!
)
The output is like this.
The source file is file://C:\TEMP\file_1.xlsx , the upload status was OK
The source file is file://C:\TEMP\file_2.xlsx , the upload status was OK
I'm expecting it to dump the altered values out into a new file but it is not producing anything at the moment.
Normally I would extract from a specific character to the end of the string with something like this but it doesn't work with my For/f loop.
%var:~7%
Any pointers or a different way of doing it greatly appreciated.
Since the part to remove seems fixed it is easier to use substrings.
Also using for /f "skip=1" evades he neccessity of the external command more +1 and another intermediate file.
#echo off & setlocal EnableDelayedExpansion
type NUL>output2.txt
for /f "skip=1 eol=| tokens=1,8 delims=," %%A in (.\upload_results.log) do (
echo The source file is %%A , the upload status was %%B
set "line=%%A"
set "line=!line:~7!"
echo(!line!>>output2.txt
echo(!line!
)
File names and paths can contain also one or more exclamation marks. The line set line=%%A is parsed by Windows command processor a second time before execution with enabled delayed expansion. See How does the Windows Command Interpreter (CMD.EXE) parse scripts? Every ! inside the string assigned to loop variable A is on this line interpreted as begin or end of a delayed expanded environment variable reference. So the string of loop variable A is assigned to environment variable line with an unwanted modification if file path/name contains one or more exclamation marks.
For that reason it is best to avoid usage of delayed expansion. The fastest solution is for this task using a second FOR to get file:// removed from string assigned to loop variable A.
#echo off
del output2.txt 2>nul
for /F "skip=1 tokens=1,8 delims=," %%A in (upload_results.log) do (
echo The source file is %%A , the upload status was %%B.
for /F "tokens=1* delims=/" %%C in ("%%~A") do echo %%D>>output2.txt
)
Even faster would be without the first echo command line inside the loop:
#echo off
(for /F "skip=1 delims=," %%A in (upload_results.log) do (
for /F "tokens=1* delims=/" %%B in ("%%~A") do echo %%C
))>output2.txt
The second solution can be written also as single command line:
#(for /F "skip=1 delims=," %%A in (upload_results.log) do #for /F "tokens=1* delims=/" %%B in ("%%~A") do #echo %%C)>output2.txt
All solutions do following:
The outer FOR processes ANSI (fixed one byte per character) or UTF-8 (one to four bytes per character) encoded text file upload_results.log line by line with skipping the first line and ignoring always empty lines and lines starting with a semicolon which do not occur here.
The line is split up on every occurrence of one or more commas into substrings (tokens) with assigning first comma delimited string to specified loop variable A. The first solution additionally assigns eighth comma delimited string to next loop variable B according to ASCII table.
The inner FOR processes the string assigned to loop variable A with using / as string delimiter to get assigned to specified loop variable file: and to next loop variable according to ASCII table the rest of the string after first sequence of forward slashes which is the full qualified file name.
The full qualified file name is output with command echo and appended either directly to file output2.txt (first solution) or first to a memory buffer which is finally at once written into file output2.txt overwriting a perhaps already existing file with that file name in current directory.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
del /?
echo /?
for /?
See also the Microsoft article about Using command redirection operators for an explanation of the redirections >, >> and 2>nul

Why does for /f not ignore blank lines?

I'm trying to get a simple value from a for /f loop in a batch file.
Using the command wmic path win32_localtime get dayofweek gives me the output:
C:\sendemail>wmic path win32_localtime get dayofweek
DayOfWeek
1
C:\sendemail>
So, using the following batch script, I should be able to return a result of "1":
set cmd=wmic path win32_localtime get dayofweek
for /f "tokens=1 skip=1" %%Z in ('%cmd%') do set _myday=%%Z
echo Var is %_myday%
But I don't, it sets the variable at least twice, as seen here :
C:\sendemail>set cmd=wmic path win32_localtime get dayofweek
C:\sendemail>for /F "tokens=1 skip=1" %Z in ('wmic path win32_localtime get dayofweek') do set _myday=%Z
C:\sendemail>set _myday=1
:\sendemail>set _myday=
C:\sendemail>echo Var is
Var is
C:\sendemail>
At first, I wondered why, then I realised the loop is processing the two blank lines... which it shouldn't be. according to this: http://ss64.com/nt/for_cmd.html
Skip SKIP will skip processing a number of lines from the beginning of
the file. SKIP includes empty lines, but after the SKIP is complete,
FOR /F ignores (does not iterate) empty lines.
Either, it's not working normally, or those lines are not blank, and are filled with something...
At any rate, how do I get just the day of the week result out of this, and ignore the rest?
About the lines contents, yes, the output from wmic includes at the end of each line an additional carriage return character that is detected by the for command. That is the reason for your non empty lines.
You can try with
for /f "tokens=2 delims==" %%Z in ('
wmic path win32_localtime get dayofweek /value
') do for /f %%Y in ("%%Z") do set "_myday=%%Y"
The code is asking wmic to return the information in key=value format and using the equal sign to tokenize the input records. As we request only the second token, for will only process lines that return at least two tokens, lines having a key (first token), an equal sign (delimiter) and a value (second token)
But, the additional carriage return at the end of the line is also included in the value of the variable.
When the variable is used with normal syntax (%_myday%) it is not seen, but with delayed expansion (!_myday!) the additional carriage return will be echoed.
To remove this additional character from the variable, a second for command has been used in the posted code.
This slight midification will work:
#ECHO OFF
set cmd=wmic path win32_localtime get dayofweek
for /f "tokens=1 skip=1" %%Z in ('%cmd%') do (
set _myday=%%Z
GOTO BREAK
)
:BREAK
echo Var is %_myday%
PAUSE
Simply jump out of the loop after reading the desired line.
The above is a good approach and intitially I applied it successfully. Because of another need to capture a WMI variable and load into an environment variable, I wanted some that would be a native command line. I stumbled on this...
for /F "skip=2 tokens=2 delims=," %%i IN ('wmic timezone get Bias /format:csv') do (echo var Bias=%%i; > %TZONEDB%mid.htm )
The core is that using the CSV format makes skip work (no trailing blanks) and gives you easily parsed output.
ed g
I recently had the issue discussed here and whilst previous answers helped I wanted to retrieve several variables for subsequent testing. At first I used code similar to the following example (just not on explorer.exe and with different variable names. The following variable names have been deliberately chosen to match variable name restrictions in the later code).
setlocal
for /F "skip=2 tokens=2,3,4 delims=," %%a in ('
wmic /node:"MyFileServer-pc" process where Name^="explorer.exe"
get HandleCount^,
ParentProcessId^,
ThreadCount
/format:csv
') do (
set ProcHandleCount=%%a
set ProcParentProcessId=%%b
set ProcThreadCount=%%c
)
echo HandleCount %ProcHandleCount%, ParentProcessID %ProcParentProcessId%, ThreadCount %ProcThreadCount%
However, I dislike this solution as wmic always returns results in the order in which it internally iterates them disregarding the order in which they are presented to the wmic command, So the command wmic process where Name^="explorer.exe" get ThreadCount^,HandleCount^,ParentProcessId still returns ThreadCount last. This makes the previous code error prone when fetching multiple parameter value pairs and a hassle to get the ordering correct.
So instead I used the following code, which achieves the same result avoiding the need to care about the ordering of wmic parameters. It is also very easy to add additional parameters still with no need to care about ordering.
setlocal
for /F "tokens=1,2delims==" %%a in ('
wmic /node:"MyFileServer-pc" process where Name^="explorer.exe"
get HandleCount^,
ParentProcessId^,
ThreadCount
/value
') do #set Proc%%a^=%%b
echo HandleCount %ProcHandleCount%, ParentProcessID %ProcParentProcessId%, ThreadCount %ProcThreadCount%
In the above code, the variable naming is provided by the parameter of each returned parameter=value pair from wmic plus a fixed prefix, automatically matching returned parameter and value pairs. Note the use of a prefix, in this case 'Proc', without at least a one character prefix returned wmic blank lines cause errors. If different environment variable names are needed then the first solution allows freedom in naming and allows for shorter names but is more error prone when multiple values are required.
For what little it's worth... the following code avoids the variable prefixes and silences errors. More unique variable names would usually be desired.
(
for /F "tokens=1,2delims==" %%a in ('
wmic /node:"MyFileServer-pc" process where Name^="explorer.exe" get HandleCount^,ParentProcessId^,ThreadCount /value
') do #set %%a=%%b
) 2>nul
These examples use the wmic parameter /node:"MyFileServer-pc" to get remote process info but work equally for simpler wmic commands.
I always use this batch template when processing wmic output:
setlocal enableextensions enabledelayedexpansion
for /f "delims and such" %%a in ('wmic path win32_localtime get dayofweek 2^>nul ^<nul') do (
set VAR=%%a
CALL :RMCRLF VAR
if "!VAR!" NEQ "" (
rem If we get here, then it's a non-empty line
rem ... Do something ...
)
)
GOTO :EOF
:RMCRLF
:: All this does is clean stray carriage returns/line feeds off of a variable,
:: which wmic is notorious for injecting into its output.
set RMCRVAR=%1
IF NOT DEFINED RMCRVAR GOTO :EOF
CALL set %RMCRVAR%=%%%RMCRVAR%%%
GOTO :EOF
They key here is the "function" (if you will) :RMCRLF. It will strip the pesky carriage returns off of a variable so you can check for true emptiness of the string, and then decide to skip the empty line.

File path in Windows XP NTFS Format with batch for /F command

i try to figure out if i recognize enough reading the help and support center with this question.
Some informations are not linked in a for me reasonable way.
Reading about the Type command it was possible for me to recognize that using NTFS Format with my hard disk ,i have to use double quotes for path and file names with spaces.
I will show a batch code first without quotes and then with ,because with a for /F command this case double quoted do not match either with a echo command nor a type command.
#echo off
rem #
rem #
rem #
for /F %%A in (Textdokument.txt) do set Datei=%%A
rem #
rem #
echo %Datei%
rem #
rem #
echo.
rem #
rem #
set Datei=
rem #
rem #
pause
Workes proper.
The same example with double quotes just returns a ,file not found message.
for /F %%A in ("Dokumente und Einstellungen"\Benutzername\Desktop^\"Neuer Ordner"\Textdokument.txt) do set Datei=%%A
Building an easy base for gathering more reasonable file content leads to this question regarding all information i have read. To search further i would be lucky with a answer.
Best wishes
for /f %%a in (someFileName) ... will consider someFileName as a file name to read, executing the code in the do clause for each of the lines in the file.
for /f %%a in ("some file name") .... will consider "some file name" as a inmediate string to process, executing the code in the do clause for only this string.
for /f "usebackq" %%a in ("some file name") ... will consider "some file name" as a file name to read, executing the code in the do clause for each of the lines in the file.
And, the quotes should enclose the full path, so your code should be
for /F "usebackq" %%A in (
"Dokumente und Einstellungen\Benutzername\Desktop\Neuer Ordner\Textdokument.txt"
) do set Datei=%%A
Try like this with the full path and with just a " at start and one at the end and using type:
for /F %%A in (type "c:\Dokumente und Einstellungen\Benutzername\Desktop\Neuer Ordner\Textdokument.txt") do set Datei=%%A

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