Batchjob for changing the date format in a filename - batch-file

hope somebody can help me.
I need a .bat doing the following:
Run in folder "A" all the time
waiting for a file to appear in the following format:
1_33530_Jim_Hutchinson_m_23.04.1965_20210628-163636_D-Endo14.pdf
reformatting the file to
1_33530_Jim_Hutchinson_m_19650423_20210628-163636_D-Endo14.pdf
write the new file to folder B
Meaning in the filename the date format should change from DD.MM.YYYY to YYYYMMDD everything else
before and after should be untouched
How can I do that with a looping batch job?
Kind regards and thanks in advance
Sven

Split filename 1_33530_Jim_Hutchinson_m_23.04.1965_20210628-163636_D-Endo14.pdf to three part. Delimiter is point.
Using string substitute split 1_33530_Jim_Hutchinson_m_23 to 1_33530_Jim_Hutchinson_m_ and 23
set filename1=!temp_var1:~0,-2!
set day=!temp_var1:~-2!
Using string substitute split 1965_20210628-163636_D-Endo14 to 1965 and _20210628-163636_D-Endo14
set year=!temp_var2:~0,4!
set filename2=!temp_var2:~4!
Make new filename
set new_filename=!filename1!!year!!month!!day!!filename2!.pdf
full code
setlocal ENABLEDELAYEDEXPANSION
for %%f in (*.pdf) do (
for /f "delims=. tokens=1,2,3" %%a in ("%%f") do (
set temp_var1=%%a
set month=%%b
set temp_var2=%%c
set filename1=!temp_var1:~0,-2!
set day=!temp_var1:~-2!
set year=!temp_var2:~0,4!
set filename2=!temp_var2:~4!
set new_filename=!filename1!!year!!month!!day!!filename2!.pdf
copy %%f B\!new_filename!
)
)

Related

Change date format from file name (ddmmyyyy) into dd/mm/yyyy

Following from previous code, previous link I would like to improvise my code by changing date format which is ddmmyyyy which I get from file name then stored this data into CSV file.
Is it easier to change from file name or inside csv file?
File name
10092019.LOG
11092019.LOG
12092019.LOG
Current code
#echo off
cls
setlocal EnableDelayedExpansion
set /a total=0
type NUL>Report.csv
(
echo Station Number,Date ,Done
for /R "%userprofile%\Google Drive\Report BACKUP\" %%G in (*.LOG) do (
for %%b in ("%%~dpG\.\..") do set station= %%~nxb
for /f %%a in ('type "%%G"^|find /C /v "" ') do set /a total+=%%a&echo !station!,%%~nG ,%%a
)
echo ,TOTAL ,!total!
)>>Report.csv
GOTO :EOF
CSV file format contain 2 header
LASTEST UPDATE: 27/Sep/2019 12:26 , ,
STATION NUMBER,DATE ,TAG
Station1,10092019 ,11
Station1,11092019 ,491
Station1,12092019 ,205
Got the idea from here while looking for a solution. Silly me all I need to do is just adding this 2 line..
set dt= %%~nG
and
echo !dt:~0,3!/!dt:~3,2!/!dt:~5,8!
but Im still open for a suggestion or maybe more elegant way to achieve this. Thank you.

How to concatenate two variable values and use in if condition

I am trying to append the relative path of a file to a root path to get the complete path of the file. The same file, i am trying to check if exists , then trying to rename it to todays date . I am using the below code. While exhoing the concatenated values in out1.txt i am getting correct values. But in if condition ,I am getting syntax error on concatenating the values of two variables here. I think i am doing something wrong Can anyone please help . Thanks a lot.
echo on
setlocal enabledelayedexpansion
set rootPath=C:\Endeca
rem copying to index
set indexTarget=%rootPath%
set IndexFilePath=Tiffany\ENI\Endeca
set MDEXFilePath=Tiffany\ENE\Endeca
cd %IndexFilePath%
set "parentfolder=%CD%"
for /r . %%g in (*.*) do (
set "completepath=%%g"
set relativePath=!completepath:%parentfolder%=!
echo !rootPath!!relativePath! >> out1.txt
if exist !rootPath!!relativePath!
for /f "tokens=1-5 delims=/ " %%a in ("%date%") do rename "!%rootPath%%relativePath%!" %rootPath%%relativePath%.%%b-%%c-%%d
)
Here's a completely untested rewrite of your code, based upon my understanding of what you're trying to do.
The main issue I could see with it was that your if condition did not use parentheses to isolate the command(s) specific to it.
#Echo Off
SetLocal DisableDelaydExpansion
Set "rootPath=C:\Endeca"
Set "dStamp="
For /F "Tokens=1-3Delims=/ " %%A In ('RoboCopy/NJH /L "\|" Null'
) Do If Not Defined dStamp Set "dStamp=%%A-%%B-%%C"
Rem Copying to index
Set "indexTarget=%rootPath%"
Set "IndexFilePath=Tiffany\ENI\Endeca"
Set "MDEXFilePath=Tiffany\ENE\Endeca"
CD "%IndexFilePath%"
Set "parentfolder=%CD%"
For /R %%A In (*) Do (
Set "completepath=%%~nA"
SetLocal EnableDelayedExpansion
Set "relativePath=!completepath:%parentfolder%=!"
(Echo %rootPath%!relativePath!%%~xA)>>"out1.txt"
If Exist "%rootPath%!relativePath!%%~xA" (
Ren "%rootPath%!relativePath!%%~xA" "%rootPath%!relativePath!.%dStamp%%%~xA"
)
EndLocal
)
You'll note that I have moved the for-loop, designed to get the date string, from its nested position to nearer the start of your code. I have assumed that this was supposed to be in YYYY-mm-DD format, you'll need to adjust %%A-%%B-%%C on line 6 if that assumption was incorrect.

Looking for a script to rename multiple files with charactares already in filename

I have many files in many folders that I need to rename.
And example is
from cgs2016-09-05-05-40-34.xls
to cgs0905.xls
and
from cgs2016-09-06-05-40-34
to cgs0906.xls
etc
Any help would be greatly appreciated!
#Jamaz Try out the following code below on a sample of your files. Again please use it on a test sample of your files so it does not cause you issues if it makes a mistake. Thank you and please up-vote if this works for you.
setlocal enabledelayedexpansion
REM Collect list of file names in current directory that have the .xls file extension. Output to text file.
dir /b "*.xls" >xls.txt
REM For loop examines the text file for individual file names.
FOR /F "tokens=1" %%# in (.\xls.txt) do (
REM SET variable "#" to equal "token"
Set "token=%%#"
REM Extract the first 3 characters (year) from the file name and set is to variable "token"
Set "tokenchar=!token:~0,3!"
REM Extract the month characters from the file name and set the variable as "tokenmonth"
Set "tokenmonth=!token:~8,2!"
REM Extract the day characters from the file name and set the variable as "tokenday"
Set "tokenday=!token:~11,2!"
ren "%%#" "!tokenchar!!tokenmonth!!tokenday!.xls"
echo %%#
)
Pause
not the best way, but works for your examples:
#echo off
setlocal enabledelayedexpansion
for %%x in (*.xls) do (
set "filename=%%x"
ECHO ren "%%x" "!filename:~0,3!!filename:~8,2!!filename:~11,2!.xls"
)
remove the ECHO if output is ok.
Because the last nineteen characters, date and time stamp, are more likely to be constant than the first three, (especially over multiple folders), I'd change both the previous answers to cater for that rationale.
#Echo Off
SetLocal EnableDelayedExpansion
(Set _rf=C:\Users\jamaz\TestDir)
(Set _fe=xls)
If Not Exist "%_rf%\" Exit/B
For /R "%_rf%" %%I In (*.%_fe%) Do (Set "_fn=%%~nI"
Echo=Ren "%%I" "!_fn:~,-19!!_fn:~-14,2!!_fn:~-11,2!%%~xI")
Timeout -1 1>Nul
EndLocal
Exit/B
As the OP was not clear about whether the code was for multiple same level folders or subfolders rooted from a single location, I went for the latter as the previous responses had already covered that.
Change your chosen file path and extension on lines 4 and 5
If you are happy with the console output, remove echo= from line 10 and delete line 11

Batch List files in folder and subfolder according to specific format

I am having an question
Is it possible to make an batch file that read the dir and subdirs and output the results in an specific format into an .txt file?
I know how to read the dir including sub dirs etc.
But what i dont know is how i can use an specific format to an .txt file.
Example i have
c:\patch
c:\patch\subfolder
c:\patch\subfolder\subfolder
In those folders i have some files
What i need is that the output will be something like this
2014-05-17 03:16p 7155712 file.exe
2014-05-11 03:41p 19287692 subfolder\file1.res
2014-05-11 03:42p 35508951 subfolder\subsubfolder\file1.res
etc.
With kind regards,
Thomas de Vries.
EDIT: Small modifications as reply to the comments.
The following two points have been modified:
Convert date from DD-MM-YYYY to YYYY-MM-DD.
Minor adjustments in the output format.
.
#echo off
setlocal EnableDelayedExpansion
set "base=%CD%"
set "base=%base:*:=%\"
for /R %%a in (*.*) do (
for /F "tokens=1-5 delims=/-. " %%b in ("%%~Ta") do set "dateTime=%%d-%%c-%%b %%e%%f"
set "size= %%~Za"
set name=%%~PNXa
echo !dateTime! !size:~-19! !name:%base%=!
)

batch script change line

I need to change the date in a batch file, this line is a parameter for backup software.
The file with the paramentros:TEXT.INI
[indexa]
testindex=
[Restaurante_Geral]
FWHCOMPPREMEDIO=
[backup]
ultbackp=05/08/13
I need to change the line ultbackp=05/08/13, to current date.
I use the batch file below, but does not change the date if the parameter is already set.
#echo off
SETLOCAL=enabledelayedexpansion
SET YY=%DATE:~8,2%
SET MM=%DATE:~3,2%
SET DD=%DATE:~0,2%
rename text.ini text.tmp
for /f %%a in (text.tmp) do (
set foo=%%a
if !foo!==ultbackp set foo=ultbackp=%DD%/%MM%/%YY%
echo !foo! >> text.ini)
del text.tmp
Can anyone help me?
try this:
if "!foo:ultbackp=!" neq "!foo!" set "foo=ultbackp=%DD%/%MM%/%YY%"
ultbackup is not set to anything. Therefore your compare is failing.

Resources