change datetime text on files - batch-file

I have this format on jpg files but i need the time and date like this which I am not able to get this.
present format
1UYK08HJ_20140403165858071_SYPTE1-PC.jpg
1YK0BHJX_20140403165902791_SYPTE1-PC.jpg
1YK08HJX_20140403165959270_SYPTE1-PC.jpg
IDYKDBH_20140403170236634_SYPTE1-PC.jpg
required format is
1UYK08HJ_SYPTE1-PC_2014_04_03_16_58_58_071.jpg
1YK0BHJX_SYPTE1-PC_2014_04_03_16_59_02_791.jpg
1YK08HJX_SYPTE1-PC_2014_04_03_16_59_59_270.jpg
IDYKDBH_SYPTE1-PC_2014_04_03_17_02_36_634.jpg
my query is :
#echo off
pushd "C:\Users\IT-Administrator\Desktop\export" || exit /b
for /f "tokens=1-4 delims=_." %%A in ('dir /b /a-d *_*.jpg') do (
Echo ren %%A_%%B_%%C.%%D %%A_%%C_%%B.%%D
)
popd

The method to separate : 20140403165858071 in 2014_04_03_16_58_58_071
#echo off&cls
setlocal EnableDelayedExpansion
for /f "tokens=1-4 delims=_." %%a in ('dir /b/a-d *.JPG') do (
set $Char=%%b
set $CharF=!$Char:~0,4!_!$Char:~4,2!_!$Char:~6,2!_!$Char:~8,2!_!$Char:~10,2!_!$Char:~12,2!_!$Char:~14!
echo !$CharF!
pause)
You can then adjust yourself the REN with %%a_!$CharF!_%%c_%%d or the way you want it

Related

Specific use of double loop

I recently asked you guys some help for a double conditions in batch file. You guys helped me very well but I'm now struggling with a new trouble... I'm trying to optimize my script.
Previous question (How to double conditions in batch file?)
I would like to list all files with the .doc extension that are in the subfolders named on the current date only. I think I'm making a mistake on the use of this double loop.
#echo off
chcp 1252
set Pathname="D:\testDir"
set year=%date:~-4%
set month=%date:~-7,2%
set day=%date:~-10,2%
set logfile=%Pathname%\logs\log.txt
cd %Pathname%
d:
for /D /R %%i in (*%year%_%month%_%day%*) do (
for /R %%i %%s in (*.doc) do (
echo "file : %%s worked and does an output at %time%" >> %logfile%
)
)
Can you give me a little help? Thanks for your help and advices.
EDIT: I need to perform actions on each .doc file in folders containing the string YEAR_MONTH_DAY. But the .doc files can also be located in other subdirectories.
It could be something like that :
D:\testDir\directory1_2021_11_16\test.doc
D:\testDir\directory1_2021_11_16\test.log
D:\testDir\directory1_2021_11_16\subDirectory1\test.doc
D:\testDir\directory1_2021_11_16\subDirectory1\test.log
D:\testDir\directory1_2021_11_17\test.doc
D:\testDir\directory1_2021_11_17\test.log
D:\testDir\directory1_2021_11_17\subDirectory1\test.doc
D:\testDir\directory1_2021_11_17\subDirectory1\test.log
D:\testDir\directory1_2021_11_17\subDirectory2\test.doc
D:\testDir\directory1_2021_11_17\subDirectory2\test.log
D:\testDir\directory1_2021_11_17\subDirectory2\subSubDirectory1\test.doc
D:\testDir\directory1_2021_11_17\subDirectory2\ubSubDirectory1\test.log
D:\testDir\directoryThatIDontCare\test.doc
D:\testDir\directoryThatIDontCare\test.log
D:\testDir\directoryThatIDontCare\subDirectory1\test.doc
D:\testDir\directoryThatIDontCare\subDirectory1\test.log
Any solutions ? Thanks for your time guys !
Here's an example script you can learn from, and run, to achieve what your submitted code was intending to do.
#Echo Off
SetLocal EnableExtensions
Set "CurDate="
For /F "Delims==" %%G In ('"(Set _) 2>NUL"') Do Set "%%G="
For /F "Delims=" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe Path
Win32_LocalTime Get Day^, Month^, Year /Format:List
2^>NUL') Do For /F "Tokens=*" %%H In ("%%G") Do Set /A "_%%G + 10000"
For /F "Tokens=1,* Delims==" %%G In ('"(Set _) 2>NUL"') Do (
SetLocal EnableDelayedExpansion & If %%H Gtr 10059 (For %%I In (!%%G:~-4!
) Do EndLocal & Set "%%G=%%I") Else For %%I In (!%%G:~-2!
) Do EndLocal & Set "%%G=%%I")
Set CurDate=%_Year%_%_Month%_%_Day%
For /F "Delims==" %%G In ('"(Set _) 2>NUL"') Do Set "%%G="
If Not Defined CurDate GoTo :EOF
Set "BaseLocation=D:\testDir"
PushD "%BaseLocation%" 2>NUL || GoTo :EOF
Dir "logs" /B /A:D 1>NUL 2>&1 || (MD "logs\%CurDate%" 2>NUL || GoTo :EOF)
Set "LogFile=%BaseLocation%\logs\log.txt"
For /F "EOL=? Delims=" %%G In ('Dir "*_%CurDate%" /B /A:D 2^>NUL'
) Do For /F Delims^= %%H In ('Set "PATHEXT^=" ^& %SystemRoot%\System32\where.exe
/F /R "%%G" "test.doc" 2^>NUL'
) Do (Echo file : %%~H worked and does an output at %TIME%) 1>>"%LogFile%"
If you want to know how any of it works, please use the built-in help information for each command, use the site search facility and/or your chosen search provider. I am not a private tutor, so will not be performing such a role.

batch file remove X characters of filename

I did batch file which copy 3 files and need to rename it by removing last 33 characters. The copy works fine but removing last 33 characters not... I saw more then one answer on web and try it all but nothing work so far.
My batch file look like this:
for /f "delims=" %%i in ("my folder") do (
ren "%%i" "%i:~0,-33%".txt
)
I tried already:
set fName=%%i
ren "%fName%" "%fName:~0,-33%.txt"
From the information I got here, try this:
#echo off
setlocal enabledelayedexpansion
set "folderpath=[Your Folder Here...]"
cd %folderpath%
for /f %%a in ('dir /b "*.txt"') do (
set "fname=%%~na"
ren "%%a" "!fname:~0,-33!.txt"
)
endlocal
This is similar to the answer above. You should make sure the batch file is OUTSIDE the folder.
EDIT.
When dealing with variables formed inside FOR and IF's, use delayed expansion (i.e. !var!, instead of %var%). Anyway, this is the fixed code:
#echo off
setlocal enabledelayedexpansion
::NO Last Backslash...
set "sourcepath=C:\Users\tzahi.k\Desktop\testSource\source2"
set "folderpath=C:\Users\tzahi.k\Desktop\testSource\des"
for /F "delims=" %%a in ('dir /b /od "%sourcepath%\*.txt"') do (
set "youngest=%%a"
xcopy /y "%sourcepath%\!youngest!" "%folderpath%"
)
cd /d %folderpath%
for /f %%a in ('dir /b "*.txt"') do (
set "fname=%%~na"
ren "%%a" "!fname:~0,-33!.txt"
)
endlocal
pause
Here's the batch file you'd want to run:
#echo off
Setlocal EnableDelayedExpansion
#for /f "delims=" %%i in ('dir /b *.txt') do (
set fname=%%~ni
set fname=!fname:~0,-33!.txt
ren "%%i" "!fname!"
)
endlocal
This should work
#echo off
setlocal enabledelayedexpansion
set FOLDER_PATH=C:\Some\Path\
for %%f in (%FOLDER_PATH%*) do if %%f neq %~nx0 (
set "filename=%%~nf"
ren "%%f" "!filename:~0,-33!%%~xf"
)
PAUSE
Or better this
#echo off & setLocal enableDELAYedeXpansion
for /f "tokens=* delims= " %%a in ('dir /b *.txt') do (
set F=%%~Na
set F=!F:~0,33!
move /y "%%a" "!F!%%~Xa"
)

Create List of file for a directory

I have a directory of files for example:
\1.gif
\1.pdf
\1.doc
\2.gif
\2.pdf
\2.doc
\3.gif
\4.gif
How do I get a file with just the list of files (with extensions) that have unique filenames? (i.e. 3.gif, 4.gif)
Thanks
This does not work with special characters like !, = and more.
#ECHO OFF &SETLOCAL
for %%a in (*) do set ".%%~na=%%~xa"& set /a $%%~na+=1
for /f "tokens=1*delims==." %%a in ('set .') do for /f "tokens=2delims==" %%c in ('set "$%%a"') do if %%c==1 echo(%%a.%%b
i havent tested it, but try this:
#echo off
setlocal enabledelayedexpansion
pushd path\to\dir
for /f %%i in ('dir /b *.gif') do (
find "%%~ni" "%tmp%\u" >nul 2>&1 || echo %%~ni >> "%tmp%\u"
)
for /f %%i in ('type "%tmp%\u"') do (
set inc=
for /f %%j in ('dir %%i.gif /b') do set /a inc+=1
if !inc! EQU 1 (dir /b %%i.* ^|find "log" /v >> log)
)
please feel free to comment if there are any mistakes, not in a position to test it right now.
This will work as long as none of the file names contain the = character:
#echo off
setlocal
for /f "eol== delims==" %%V in ('"set _ 2>nul"') do set "%%V="
for %%F in (*) do if not defined _%%~nF (set "_%%~nF=%%F") else set "_%%~nF=="
for /f "eol== tokens=2 delims==" %%F in ('"set _|findstr /v =="') do #echo %%F
EDIT
If you want to restrict output to only one file type, .gif for example, then all you need is an extra FINDSTR:
#echo off
setlocal
for /f "eol== delims==" %%V in ('"set _ 2>nul"') do set "%%V="
for %%F in (*) do if not defined _%%~nF (set "_%%~nF=%%F") else set "_%%~nF=="
for /f "eol== tokens=2 delims==" %%F in ('"set _|findstr /v ==|findstr /lie .gif"') do #echo %%F

Order files list by batch

I have a long list of individual songs not in folders just songs, and I'd like to move them to the folder of theire artist. the songs are in the following format
artist - songname.flac
I can store them in a list, and echo it, but splitting the artist and songname in 2 vars, I can't seem to figure out.
Could someone help me with the splitting (or if you want even with the rest of the script)
this is what I have so far:
#echo off
setlocal enabledelayedexpansion
set N=0
for %%i in (*) do (
set Files[!N!]=%%~ni
set /a N+=1
)
for /l %%x in (1,1,%N%) do echo.!Files[%%x]!
pause
set SOURCE=c:\temp\test
for /f "delims=-. tokens=1,2" %%i in ('dir /b "%SOURCE%\*.flac"') do echo Artist : %%i Song : %%j
update for full script (got to check if it works with space and special chars in path) :
#echo off
setlocal enabledelayedexpansion
set SOURCE=c:\temp\test
set DESTINATION=c:\temp\test
for /f "tokens=*" %%i in ('dir /b "%SOURCE%\*.flac"') do call :OrderThatMess "%%i"
:OrderThatMess
set NAME=%1
for /f "tokens=1,2 delims=-. " %%j in (%1) do (
set ARTIST=%%j
set TITLE=%%k
if not exist "%DESTINATION%\%ARTIST%" (md "%DESTINATION%\%ARTIST%" )
copy %SOURCE%\%NAME% "%DESTINATION%\%ARTIST%\%TITLE%.flac"
)
#echo OFF &SETLOCAL ENABLEDELAYEDEXPANSION
for %%i in (*.flac) do (
set /a N+=1
FOR /f "tokens=1,2 delims=- " %%o IN ("%%~ni") DO (
set "FilesA[!N!]=%%~o"
set "FilesB[!N!]=%%~p"
)
)
for /l %%x in (1,1,%N%) do echo(!FilesA[%%x]! !filesB[%%x]!
Thanks for the answers ;) Thanks to Kayasax I fixed it
here is the full code:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set SOURCE=C:\music\folder\with\files\
for /f "tokens=1,2 delims=-" %%i in ('dir /b "%SOURCE%\*.flac"') do (
set "folder=%%i"
IF NOT EXIST "%SOURCE%\%%i" (
mkdir "%%i"
)
move "%SOURCE%\%%i-%%j" "%SOURCE%\!folder:~0, -1!\%%i-%%j"
)
pause

How to get the most recent file using a batch file?

I have a list of zip files with date and time appended like yyyymmdd_hhmmss_Demos.zip. Now how to get the most recently added zip file in the source dir. I need to copy this file in the target using copy command.
I found some info about forfiles, but do not have an idea on how to get it done for seconds.
You can use
pushd D:\a
for /f "tokens=*" %%a in ('dir /b /od') do set newest=%%a
copy "%newest%" D:\b
popd
set Path="D:\hello\abc\old"
for /f "tokens=*" %%a in ('dir /A:-D /B /O:-D /S %Path%') do set NEW=%%a&& goto:n
:n
echo %NEW%
pushd \\ryap\CONTROL_DATOS
for /f "tokens=*" %%a in ('dir \\ryap\CONTROL_DATOS /b /od') do set newest=%%a
Xcopy/Y "\\ryap\CONTROL_DATOS\%newest%" "D:\TXT_SOURCES\"
popd
Below snippet will extract the date and customize as per your needs
for /f "tokens=1-4 delims=/ " %%i in ("%date%") do (
set dow=%%i
set month=%%j
set day=%%k
set year=%%l
)
:: Pad digits with leading zeros e.g Sample_01-01-21.csv
set yy=%year:~-2%
set datestr=%day%-%month%-%yy%
Alternate way:
set datestr=%date:~0,2%-%date:~3,2%-%date:~6,2%

Resources