How to exclude the bat-file itself from the renaming - windows

following scenario:
i have written a batch script whitch adds the datestamp to the filename. There are lots of different file-types in the folder.
#echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "datestamp=%YYYY%-%MM%-%DD%_"
:: naming like ISO 8601
pause
for %%i in (*.*) do ren %%i %datestamp%%%i*
is there a good way to exclude the bat-file itself from the renaming? It would be good enought if there is a way to exclude all .bat-files.

Your trailing * in the REN statement is not doing anything - it is not needed.
You should enclose the REN filenames in quotes in case you run into names with spaces.
Filtering out (skipping) your running batch file is easy. DIR /B can be piped to FINDSTR /V to get a list of files except for the running script. %~nx0 gives the file name and extension of the running script. FOR /F is used to capture and process the list of files.
for /f "eol=: delims=" %%F in ('dir /b /a-d ^| findstr /vixc:"%~nx0"') do ren "%%F" "%datestamp%%%F"

Related

How to change multiple file names according to there names along with date

I have 7 files. in bat file I want to check name of files and then raname with current date. just like an if/else condition
Example
abc.txt rename to xyz20190715_1.txt (xyzyyyymmdd_1.txt)
pqr.txt rename to def20190715_1.txt (defyyyymmdd_1.txt)
I'm new to batch files and tried this:
for /f "tokens=1-5 delims=/ " %%d in ("%date%") do rename
"abc.txt" %%e-%%f-%%g.dat
Expected output
abc.txt rename to xyz20190715_1.txt
pqr.txt rename to def20190715_1.txt
"to loop through files" is something like
for %%I in (*.txt) do echo %%I
that's why I asked how to know the substitutions.
As there seems to be no rule, you have to rename each file individually (no big harm with only 7 files, but with a hundred ore more it would drive you crazy). I also used a differnt way to get the date string (independent of locale settings):
#echo off
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set "datetime=%%I"
set "datetime=%datetime:~0,8%"
ren "contract.txt" "DNSM%datetime%_x.DAT"
ren "security.DAT"" "NSM%datetime%_x.DAT"
ren "Participant.txt" "PM%datetime%_x.DAT"
ren "SCRIP_master.txt" "BSM%datetime%_x.DAT"
ren "DPRxxxx" "BSE_YearlyHL%datetime%_x.DAT"
ren "fo_participant.txt" "DPM%datetime%_x.DAT"
ren "spd_ contract.txt" "SCM%datetime%_x.DAT"
If the question is to get the actual date, then for today's date it's better to do the following:
for /f %%a in ('wmic path win32_LocalTime Get Day^,Month^,Year /value') do >nul set "%%a"
set Month=00%Month%
set Month=%Month:~-2%
set Day=00%Day%
set Day=%Day:~-2%
set today=%Year%%Month%%Day%
Otherwise you need to ask a real question, which starts with "how do I"
#echo off
for /f "usebackq" %%B in (`"powershell (Get-Date).ToString('yyyyMMdd')"`) do set "datetime=%%B
echo Today's Date %datetime%
ren "contract.txt" "DNSM%datetime%_x.DAT"
ren "security.DAT"" "NSM%datetime%_x.DAT"
ren "Participant.txt" "PM%datetime%_x.DAT"
ren "SCRIP_master.txt" "BSM%datetime%_x.DAT"
ren "DPRxxxx" "BSE_YearlyHL%datetime%_x.DAT"
ren "fo_participant.txt" "DPM%datetime%_x.DAT"
ren "spd_ contract.txt" "SCM%datetime%_x.DAT"
pause
exit

Getting Parent Directory for each file from Dir Output

I have a directory with multipe levels of folders.
I am completely new to writing batch files and I am writing my first one.
Stuck for ages on trying to
find all files in the directory including sub-folder
get parent directory for each file
save as variable like %parent.filename%
I have been searching here:
https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490909(v=technet.10)
And on Google but unfortunately I am stuck.
So now I managed to save the full path of each file as variable, but I want %Folder.FileName% to return the parent directory only, not the full path.
This is the code I have been testing in the command prompt.
For /F %A in ('Dir Linkedin /A-D /s /b /o') do SET Folder.%~nxA=%~pA
EDIT
I also saw this thread
And tried this code:
FOR /F %A in ('Dir Linkedin /A-D /s /b /o') do ECHO %~nxA %~pA >>Paths.txt
FOR /F "tokens=1,2" %A in (Paths.txt) do SET Parent.%A=%~nB
But %~nxB doesn't return any value... I expected it to get the last string of the path.
FOR /F %A in ('Dir Linkedin /A-D /s /b /o') do ECHO %~nxA %~pA. >>Paths.txt
FOR /F "tokens=1,2" %A in (Paths.txt) do SET Parent.%A=%~nB
Note the extra .
The path provided by the ~p modifier terminates in \ so adding . to this means "the directory name itself as though it was a filename"
As a one-line command (within a batch, decorated by standard palaver)
#ECHO OFF
SETLOCAL
FOR /F %%A in ('Dir test* /A-D /s /b /o') do FOR /F %%S in ("%%~pA.") do SET Parent.%%~nxA=%%~nS
set parent.
GOTO :EOF
I used the filemask test* to better suit my system.
I can't imagine you'd voluntarily perpetually re-type the command, so the format for use within a batch file is shown.
I would suggest you do this as a single nested For loop from the Command Prompt and with no output file:
For /F "Delims=" %A In ('Dir /B/S/A-D "Linkedin" 2^>NUL')Do #For %B In ("%~pA.")Do #Set "Folder.%~nxA=%~nxB"
From a batch-file, perhaps this would help you out:
#Echo Off
Rem Remove any existing Folder. variables
For /F "Tokens=1*Delims==" %%A In ('Set Folder. 2^>NUL')Do Set "%%A="
Rem Set the new variables
For /F "Delims=" %%A In ('Dir /B/S/A-D "Linkedin" 2^>NUL')Do For %%B In ("%%~pA.")Do Set "Folder.%%~nxA=%%~nxB"
Rem View any returned variables
Set Folder. 2>NUL&&Pause

Batch rename in Windows based on specific match

Say I have the following files:
chromadenoisecfg5 - Copy.cpp
chromadenoisecfg5 - Copy.h
chromadenoisetest5 - Copy.cpp
chromadenoisetest5 - Copy.h
imagecfg5 - Copy.h
What I'd like to do is rename them to:
chromadenoisecfg6.cpp
chromadenoisecfg6.h
chromadenoisetest6.cpp
chromadenoisetest6.h
imagecfg6.h
What I've got so far is dir /b | findstr /i "*\b5 - Copy\b*", which lists all the files I'm interested in, but I'm not sure how to further process them in terms of renaming.
Furthermore, the solution revolves around the string 5 - Copy. How can this be achieved in a generic way, with the string passed as an argument?
Here is another batch one-liner:
for /F "delims=" %%A in ('dir /B /A:-D "*5 - Copy.*"') do #(set "NAME=%%A" & setlocal EnableDelayedExpansion & ren "!NAME!" "!NAME:5 - Copy=6!" & endlocal)
This simply replaces the partial string 5 - Copy by 6 using the sub-string replacement syntax. For this to work, delayed expansion is necessary.
If you want to enter it in command prompt (cmd) directly, use this:
for /F "delims=" %A in ('dir /B /A:-D "*5 - Copy.*"') do #(set "NAME=%A" & cmd /V /C ren "!NAME!" "!NAME:5 - Copy=6!")
Batch oneliner - UNTESTED:
for /f "tokens=*" %A in ('dir /B') do for /f "tokens=1-3 delims=-. " %a in ("%A") do ren %A %a.%c

for /f "usebackq" with spaces

I am trying to put the a .jar file path into a var... but It's killing me!
I have problems with spaces.... I tried all combinations possible with `'ยด" and no lucky...
Can't pass "%programfiles(x86)%\java" as parameter to "where" command :(
Obviously I'm not putting well the special characters, but not discover how to do it!
Also checked all options in:
Batch File: FOR /F doesn't work if path has spaces
for /f "usebackq" %%f in (''where /r '%programfiles(x86)%\java file.jar'') do set "jarpath=%%f"
for /f "usebackq" %%f in ("where /r `%programfiles(x86)%\java`" file.jar) do set "jarpath=%%f"
for /f "usebackq" %%f in (`where /r` "%programfiles(x86)%\java" file.jar) do set "jarpath=%%f"
I got it! if you need...
for /f "usebackq tokens=*" %%f in (`where /r "%programfiles(x86)%\java" file.jar`) do set jarfile=%%f

batch script delete file containining certain chars

I have almost no experience in batch, but now I need a script to delete all files that contain certain characters in their names from a folder and its subfolders on Windows 64. I only did simple things in batch like
del "C:\TEST\TEST2\*.TXT"
But I have no idea how to perform the filtering I need.
Could someone help me to achieve this using batch?
EDIT more exactly, the question is "how to tell batch to include subfolders?"
The /s switch exists on quite a few commands (including del)
del /s "C:\TEST\TEST2\*.TXT"
The help for del /? says:
/S Delete specified files from all subdirectories.
Try this:
#echo off & setlocal
set "MySearchString=X"
for /f "delims=" %%i in ('dir /b /s /a-d ^| findstr /i "%MySearchString%"') do echo del "%%~i"
Set the variable MySearchString to the character or string to search and remove the echo command, if the output is OK.
You can also specify the MySearchString for the file name only:
#echo off & setlocal
set "MySearchString=T"
for /r %%a in (*) do for /f "delims=" %%i in ('echo("%%~na" ^| findstr /i "%MySearchString%"') do echo del "%%~fa"

Resources