Batch script that separates that creates folder by month - encoding

#ECHO OFF
SETLOCAL EnableDelayedExpansion
REM Set variables
SET SOURCE=C:\My WebEx Recordings
SET DEST=\\XXXRD12\c$\WebExVideoArchive
SET 7ZIP=C:\Program Files\7-Zip\7z.exe
REM Compress local files with 7zip
ECHO ---------------------------------------------------------
ECHO BEGINNING VIDEO COMPRESSION OPERATIONS
ECHO ---------------------------------------------------------
CD /D "%SOURCE%"
FOR %%f in ("*.wrf") DO (
SET FILENAME=%%~nf
ECHO Compressing !FILENAME!
"!7ZIP!" a -t7z -aoa "!FILENAME!.7z" "%%f"
)
REM Copy compressed files
ECHO ---------------------------------------------------------
ECHO COMPRESSION COMPLETE - BEGINNING COPY OPERATIONS
ECHO ---------------------------------------------------------
REM XCOPY <source> <destination> <options>
XCOPY "%SOURCE%\*.7z" "%DEST%" /Y /V /I /R
REM Confirm successful copy, then delete originals
IF %ERRORLEVEL% EQU 0 (
ECHO Copy Operation Successful. Removing Originals...
DEL /Q "%SOURCE%\*.*"
)ELSE (
ECHO Error Detected During Copying. Please try again...Press Any Key to Exit
Pause
)
I am trying to edit this code to when we run the batch file it creates a folder based off the month of our file format. We ran this script a whole lot but forgot to make a folder called April now we have May mixed in with April.
The files are formatted like
Username-R705-2011.05.04-1601-Disconnected.7z
I was wondering if there is anyway it can go off the .04 and make a folder for that month so it will automatically put it in the folder it is needing to go into, so it will be easy to search for by month.
** would it be possible if we can not use the format that we format our files in to have it sort by file creation.

Erase everything below (and including) the line REM XCOPY <source> <destination> <options> and replace it with the following:
for /F "usebackq delims=" %%a in (`dir /b "%SOURCE%\*.7z"`) do (
SET CURRENT_FILE=%%a
REM Extract the month.
for /F "usebackq tokens=3 delims=-" %%i in ('!CURRENT_FILE!') do (
SET CURRENT_FILE_DATE=%%i
SET FILE_MONTH=!CURRENT_FILE_DATE:~-2!
SET MONTH_DEST=!DEST!\!FILE_MONTH!
)
XCOPY "%SOURCE%\!CURRENT_FILE!" "!MONTH_DEST!\" /Y /V /I /R
REM Confirm successful copy, then delete original
IF %ERRORLEVEL% EQU 0 (
ECHO Copy Operation Successful. Removing Original...
DEL /Q "%SOURCE%\!CURRENT_FILE!"
)ELSE (
ECHO Error while copying "%SOURCE%\!CURRENT_FILE!.
)
)
This code goes through every file matching %SOURCE%\*.7z, extracts the month, and then copies the file to %DEST%\<month>. Month is just the 2-digit number from the filename.

Seems like you have a folder full of files with different numeric-month values embedded in the filename. I was thinking of extracting that value from each file, but it would be more straightforward to use brute force wildcards with 12 different XCOPY commands:
XCOPY "%SOURCE%\*-*-20??.??.01-*.7z" "%DEST%\01" /Y /V /I /R
XCOPY "%SOURCE%\*-*-20??.??.02-*.7z" "%DEST%\02" /Y /V /I /R
[...]
XCOPY "%SOURCE%\*-*-20??.??.12-*.7z" "%DEST%\12" /Y /V /I /R
I may be misunderstanding the question but if you can make some assumptions about the filenames, this would work.

Related

Trying to rename files based on file size

I am trying to rename some MP4 files based on file size of mp4 files in another directory. I want to name all files with identical sizes to same name. Meaning if the file size of the source file matches the size of file in the comparison directory, the source file is renamed to whatever the compared file is named. Because both directories need to be read recursively I'm thinking it would be easier to make a list for comparison with the info in it in 2 columns by using the DIR /s /b echo %%~zs>>filesizelist.txt command giving me a list like
123456789 movie.mp4
987654321 movie2.mp4
Then pass all source mp4s to the batch file and if %%~za matches a value in first column then ren the file to the
corresponding filename. Is this the best path? I tried to script it to work on the fly and that was both a no-go and the source of my 3 day headache(plus the reference list rarely changes and is obviously easily updated). Can someone please assist me with the script?
I do some test with my mp4, and the code works, and for you perform your test, you w´ll need change/put this 2 line above with the path to your folder/directory (one to keep and another to compare), by replacing in the line code is like this:
`set "_target_to_keeped=C:\Users\ecker\Videos\Target"`
`set "_target_to_rename=C:\Users\ecker\Videos\Ren_it"`
You need add the folder where are files to keep and files to rename (if size+name match) on same lines where are the 2 lines code up in this test (sorry not explain in good English, my English is not help me). By now, is late 01:53, i need sleep... yep! so, have nice code!
#echo off && setlocal enableextensions enabledelayedexpansion
cd /d "%~dp0"
set /a _cnt_in_looping= 1 - 1
set /a _cnt_files_size= 1 - 1
set "_target_to_keeped=C:\Users\ecker\Videos\Target"
set "_target_to_rename=C:\Users\ecker\Videos\Ren_it"
cd /d "!_target_to_keeped!"
for /f "tokens=* delims=^ " %%i in ('^<nul dir /o-d /on /b "*.mp4" 2^> nul ^| find "" /v /c') do set _cnt_in_looping=%%i
for /f "tokens=* delims=^ " %%i in ('^<nul dir /o-d /on /b "*.mp4"') do (
set "_file_now_keep=%%i"
set "_file_now_keeped=%%~zi %%i"
call :_to_compare_:
)
set /a _total_files_renamed=!_cnt_in_looping! - !_cnt_files_size!
set /a _total_files_n_chang=!_total_files_renamed! - !_cnt_in_looping! * -1
echo/Total of files renamed = !_total_files_renamed!
echo/Total of files n chang = !_total_files_n_chang!
endlocal
goto :_end_of_file_:
:_to_compare_:
if not exist "!_file_now_keep!" exit /b
for /f "tokens=*" %%I in ('^<nul dir /o-d /on /b "!_file_now_keep!"') do (
set "_file_now_compare=%%~zI %%I"
set "_path_now_compare=%%~dpI"
if "!_file_now_compare!" == "!_file_now_keeped!" (
rename "!_path_now_compare!\%%I" "%%~zI %%I"
echo/ rename "%%~I" "%%~zI %%I"
if ["!errorlevel!"]==["0"] call set /a _cnt_files_size=!_cnt_files_size! + 1
timeout /t 10
)
)
exit /b
:_end_of_file_:

Rename jpg files in thousands of folders to avatar.jpg and main.jpg

I have thousands of folders with 2 jpg files in them..
For example the folder 1052 has:
4a4df84f7e8d78100ceed89b368be78d.jpg
thumb_4a4df84f7e8d78100ceed89b368be78d.jpg
I want to rename the one that begins with thumb_ to avatar.jpg and the other one to main.jpg
How to do this? Will cmd rename commands help or will I need to write a windows batch script ? If so what the the commands to be used ?
I use Windows.
Just figured out a trick.
Rename everything with thumb to xyz extension
for /R %x in (thumb*.jpg) do ren "%x" avatar.xyz
Rename remaining job file to main.jpg:
for /R %x in (*.jpg) do ren "%x" main.jpg
Rename xyz back to jpg
for /R %x in (*.xyz) do ren "%x" *.jpg
You could do all in one shot:
rem // Iterate all `*.jpg` files:
for /F "delims=" %%F in ('dir /S /B /A:-D *.jpg') do (
rem /* Extract the part of the file name before the first `_`; the leading `-` is added
rem to avoid files like `__thumb___xxx.jpg` to be renamed to `avatar.jpg`: */
for /F "delims=_ eol=_" %%I in ("-%%~nF") do (
rem // Check the extracted file name part and rename the file accordingly:
if /I "%%I"=="-thumb" (
ren "%%~F" "avatar.*"
) else (
ren "%%~F" "main.*"
)
)
)
To try this directly in command prompt, change all %% to %.

Using a Batch-Script to add a date between the filename and the extension of it

I have a Script that is searching for the newest file in the directory "test" and gives it the variable "Newest".
pushd c:\Test
for /f %%i in ('dir /b/a-d/od/t:c') do set Newest=%%I
Now let's assume that the name of the file is ThisFile.txt.
I now want to rename it to ThisFile_yyyymmdd.txt by using my variable %Newest%.
What I have so far is this:
Set TDate=%date:~6,6%%date:~3,2%%date:~0,2%
ren %Newest% "%Newest%%TDATE%"
This however renames my file to ThisFile.txt_yyyymmdd which obviously removes the extension and ruins the file.
Does anyone have a solution for this?
Keep in mind that I have to rename it by using the variable %Newest%.
Here you go . Also %%i are case sensitive.
#echo off
setlocal
pushd c:\Test
Set "TDate=%date:~6,6%%date:~3,2%%date:~0,2%"
for /f %%I in ('dir /b/a-d/od/t:c') do (
echo ren %%~fI "%%~nI_%TDATE%%%~xI"
)
exit /b 0
Must read FOR/?

Batch file to find and run latest file in a dir with only partially known name?

How can I write in a simple Windows 7 compatible Batch file:
Where filename begins with "c:\my folder\myfile*.exe", run only the most recent one created.
For example, if I have 10 files in "c:\my folder\" and they are all similarly named myfile*.exe, and myfileBOB.exe was the last of this named file to be created - how to fish this out (the folder also contains other general files of different types) automatically by filename myfile* AND created date to execute?
Many thanks!
Sort the files by date ascending, and keep the last (most recent) one.
#echo off
setlocal
pushd "c:\my folder"
set "file="
for /f "eol=: delims=" %%F in ('dir /b /a-d /od myfile*.exe') do set "file=%%F"
if defined file "%file%"
popd
Or sort files by date descending, and break out of loop after first iteration.
#echo off
pushd "c:\my folder"
for /f "eol=: delims=" %%F in ('dir /b /a-d /o-d myfile*.exe') do "%%F"&goto :break
:break
popd

Batch file: list all folders containing only a file entitled "Thumbs v0.1.db"

I need to scan through a drive and list all the folders containing only a single file, entitled "Thumbs v0.1.db". I have cobbled together the following code but it doesn't seem to work. Either the batch file exits prematurely, or it completes without listing any such subdirectories! I would be very thankful if someone could point out the problem.
#echo off
SET /P folder="Please enter root directory to seach in: "
SET writefile="C:\Users\MYNAME\Desktop\Thumbs.txt"
SET tmp="C:\Users\MYNAME\Desktop\rowcounttmp"
Echo Searching for directories, please wait...
echo Thumbs v0.1.db-only directories in %folder% > %writefile%
cd /D %folder%
for /d /r %1 %%A in (.) do (
dir /a /b "%%~fA" 2>nul | find /c /v "~StringWhichWillNotAppear~" > %tmp%
set var=<%tmp%
if [%var%] == 1 dir /a /b "%%~fA" 2>nul | findstr /i "Thumbs v0.1.db" >nul && echo %%~fA >> %writefile%
)
del %tmp%
Pause
Thanks
EDIT:
Thanks to #peter-wright, I now have this: Have I implemented his suggestions incorrectly? It seems to crash for certain filepaths (specifically the read-only drive I am trying to scan through) and there are still no results appearing.
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
Color 0A
SET /P folder="Please enter root directory to seach in: "
SET writefile="C:\Users\MYNAME\Desktop\Thumbs.txt"
SET tempfile="C:\Users\MYNAME\Desktop\rowcounttmp"
Echo Searching for directories, please wait...
echo Thumbs v0.1.db only directories in %folder% > %writefile%
cd /D %folder%
for /d /r %1 %%A in (.) do (
dir /a /b "%%~fA" 2>nul | find /c /v "~StringWhichWillNotAppear~" > %tempfile%
set var=<%tempfile%
if !var! == 1 dir /a /b "%%~fA" 2>nul | findstr /i "Thumbs v0.1.db" >nul && echo %%~fA >> %writefile%
)
del %tempfile%
Pause
The variable var is being set within a BLOCK (parenthesised series of statements.)
Any %var% within a block is replaced by the value of that var WHEN THE BLOCK IS PARSED, not when it is EXECUTED.
To access the RUN-TIME value of var use !var! AFTER having invoked delayedexpansion with a SETLOCAL ENABLEDELAYEDEXPANSION statement (probably best implemented immediately after your #echo on.
Danger, Will Robinson : DO not use tmp as a variable name - tmp is a magic variable with special meaning to batch. Others may be listed by invoking
SET
from the prompt. Also avoid DATE, TIME, RANDOM, CD and a few others. And really not a good idea to use executable names or batch keywords either...
tip:
try using
FOR /F "delims=" %%i in ('dir /s /a /b /a-d "Thumbs v0.1.db" ') do (
and then %%~dpi will be assigned the names of the directories that CONTAIN the target file. If the count-of-files in these directories is not ==1 then it is not alone...