BATCH File : archive Files per Year with special format - batch-file

I have a Folder full with files with perticular format (321456.done24) and I want to archive this files with a batch datei per year...that means to create inside this folder an another folder mit the year, if doesnt exist, and all the files from the last years - except the current year- to move them there.
an example that I have found and try to modified :`
#echo off
setlocal enabledelayedexpansion
set FolderIncoming=C:\Temp
set FileMask= *.done24
set FolderSorted=C:\Temp
for %%a in ("%FolderIncoming%\%FileMask%") do (
set FileName=%%~na
echo Processing '!FileName!' ...
set TargetFolder=!FileName:~5,8!
if not exist "%FolderSorted%\!TargetFolder!" ECHO md "%FolderSorted%\!TargetFolder!"
ECHO move "%%a" "%FolderSorted%\!TargetFolder!"
)
`
but dont know how can I create the folder with year?

With only necessary changes in your code snippet:
#ECHO OFF >NUL
#SETLOCAL enableextensions enabledelayedexpansion
set "FolderIncoming=D:\Temp"
set "FileMask=*.done24"
set "FolderSorted=D:\Temp"
pushd "%FolderIncoming%"
for %%a in ("%FolderIncoming%\%FileMask%") do (
set "FileName=%%~na"
echo Processing '!FileName!' ... %%a ... %%~ta
rem next tokens and delims valid for 'dd.mm.yyyy HH:mm:ss' datetime format
for /F "tokens=3 delims=. " %%G in ("%%~ta") do set "fileyear=%%G"
echo md "%FolderSorted%\!fileyear!" 2>nul
set "TargetFolder=!FileName:~5,8!"
if not exist "%FolderSorted%\!TargetFolder!" ECHO md "%FolderSorted%\!TargetFolder!"
ECHO move "%%a" "%FolderSorted%\!TargetFolder!"
)
popd
:: some code here
#ENDLOCAL
#goto :eof

Related

Rename files1 to fileA in all subfolders

Whenever I need something, I come here and at this moment I need something I found here but wanted more in this code:
#echo off
setlocal enableDelayedExpansion
for %%F in (*.txt) do (
set "name=%%F"
ren "!name!" "!name:file1=fileA!"
ren "!name!" "!name:file2=fileB!"
)
My question is: how to rename the files1 and file 2 in this folder and subfolders and write at the end a message "Rename: 2 files"
Thank you for your help.
to make it recursive (processing subfolders), add the /R switch.
Use a variable to build the new filename and execute the ren command only once per file.
#echo off
setlocal enableDelayedExpansion
set count=0
for /R %%F in (*.txt) do (
set "newname=%%~nxF"
set "newname=!newname:file1=fileA!"
set "newname=!newname:file2=fileB!"
ren "%%F" "!newname!"
set /a count+=1
)
echo %count% files renamed

Creating a batch file to rename TXT files based on content (removing inappropriate syntax)

I am trying to create a batch file that will allow me to rename all files in a folder based on text located within the test file itself. I have pasted the first four lines from the text below:
09-Nov-16 07:50 AM HAND-HELD DRIVER REPORT PAGE: 1
XYZ COMPANY - AR1
DATE > 11/09/16 DRIVER > 1010 DRIVER NAME > Mike Smith TRUCK > 4719
I am trying to name each folder DATE-DRIVER NAME-TRUCK. I also need to drop the "/"'s in the date field. Any help would be incredibly appreciated!
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%x IN (
'dir /b /a-d "%sourcedir%\*.txt" '
) DO (
SET "oldname=%%x"
SET "newname="
SET "drivname="
REM detect a line with 4 ">"
REM Process only the first found in a file
FOR /f "usebackqtokens=1-5delims=>" %%a IN (
"%%x") DO IF NOT DEFINED newname IF "%%e" neq "" (
REM process date into %%i, %%j, %%k
FOR /f "tokens=1-3delims=/ " %%i IN ("%%b") DO (
REM build drivname
CALL :procname %%d
REM build new filename and rename file
CALL :build %%k %%i %%j %%e
)
IF NOT DEFINED newname ECHO %%x NOT renamed
)
)
GOTO :EOF
:procname
IF "%2"=="" GOTO :EOF
SET "drivname=%Drivname% %1"
shift
GOTO procname
:build
IF NOT DEFINED drivname GOTO :EOF
SET "newname=%1%2%3-%drivname:~1%-%4"
IF /i "%oldname%" neq "%newname%" (
ECHO(REN "%sourcedir%\%oldname%" "%newname%"
)
GOTO :eof
You would need to change the setting of sourcedir to suit your circumstances.
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(REN to REN to actually rename the files.
Relatively simple:
Process each filename to %%x. With that filename, look for a line containing 4 > When that line is found (%%e will be non-empty) then the date will be in %%b, so parse it into %%i,j,k and process the name+TRUCK string in %%d. Finally, build the elements together - year in %%k, month in %%i, day in %%j, trucknumber in %%e and drivername in %drivname%.
The :build routine ensures that drivname exists and the builds the new filename from the supplied parameters and drivname. If the new and old names are the same (case-insensitive) then the rename would produce an error (file has already been processed previously) so skip the rename. Since newname is established in the :build routine, once a rename has happened, it won't be repeated.
Finally, if there was no newname set up, then the file hasn't got a line of the required format, so report that fact.
Here is my try, change BaseFld to fit your environment
#Echo off
Set BaseFld=Q:\Test\2016-11\10\
Set Files=*.txt
PushD %BaseFld%
For %%A in (%Files%) Do For /f "tokens=2-5 delims=>" %%B IN (
'findstr "DATE.*DRIVER.*DRIVER.NAME.*TRUCK" "%%~fA"'
) Do Call :Rename "%%~A" "%%B" "%%C" "%%D" "%%E"
PopD
Goto :Eof
:Rename
: Parse Date to yyMMdd
For /F "Tokens=1-3 Delims=/ " %%F in (%2) Do Set _YMD=%%H%%F%%G
For /F %%F in (%3) Do Set _DriverNo=%%F
Set _DriverName=%~4
Set _DriverName=%_DriverName: TRUCK=%
Set /A _TruckNo=%~5
Set "NewName=%_YMD%%_DriverName: =-%%_TruckNo%%~x1"
If /i "%~nx1" neq "%NewName%" Echo Ren %1 "%NewName%"
Result
Ren "Test.txt" "161109-Mike-Smith-4719.txt"
Edit To really rename the echo in the last line has to be removed. Included an if to prevent renaming an already processed file.

Moving files and renameing duplicats

I need to move files from C:\users\users name\documents\sound recordings\ directory to our network drive P:\transcription\users name\. I need the files to automatically get renamed if the file exist in the P:\transcription\users name directory. I am a novice and can only write simple batch files and the one I wrote to move the files will over write the files with the same name in the directory. Thanks
This may help.
It will display files to be moved with resulting filename. If it works ok for you , remove the line echo !src!%%~nxa - !dst!!file! and delete the rem keyword from rem move !src!%%a !dst!!file! >nul
#echo off
SetLocal EnableDelayedExpansion
set "wildcard=*.*" & rem files to search for
set "skipUsers=Guest Administrator" & rem users not to copy files
:: get users
set/a offset=0
for /F "skip=2 tokens=2 delims=," %%1 in ('wmic useraccount get name^,sid /format:csv') do (
set/a offset+=1, USER_MAX=offset
set "USER[!offset!]=%%1"
)
for /L %%i in (1,1,%USER_MAX%) do (
set/a skip=0, numFiles=0
set "currentUser=!USER[%%i]!"
set "src=C:\!currentUser!\documents\sound recordings\"
set "dst=P:\transcription\!currentUser!\"
rem get time stamp
for /f "tokens=2 delims==" %%A in ('wmic os get localdatetime /value') do set "Tm=%%A"
set "timeStamp=!Tm:~0,4!!Tm:~4,2!!Tm:~6,2!_!Tm:~8,2!!Tm:~10,2!!Tm:~12,2!"
for %%a in (%skipUsers%) do if /I "!currentUser!" equ "%%a" set/a skip=1
if !skip! neq 0 ( rem avoid copying blacklist users
echo !currentUser!: Skipping
) else (
<nul set/P="!currentUser!: Moving files"
pushd !src!
dir /ad !dst! >NUL 2>NUL || md !dst! & rem create destination folder if it doesn't exist
for %%a in (%wildcard%) do (
set "file=%%~nxa"
if exist "!dst!!file!" set "file=%%~na_!timeStamp!%%~xa" & rem rem if file exist, append a time stamp suffix to avoid overwriting
echo !src!%%~nxa - !dst!!file!
rem move !src!%%~nxa !dst!!file! >nul
set/a numFiles+=1
)
echo : !numfiles! files moved
popd
)
)
EndLocal
exit/B

Windows Batch - Find all pdf's in sub directories, exclude specific foldernames

Context
I'm currently programming with batch files, to use a specific pdf tool only available for cmd.
Problem
I'm trying to run a for loop, which recursively cycles through a directory finding all *.pdf files. Excluding the pdf's inside folders named "Originals"
If the pdf file is in a parent folder named "Originals", then it must be skipped. Otherwise count the pdf file with the %counter% variable.
Example Directory Structure
C:\New folder\file (1).pdf
C:\New folder\file (2).pdf
C:\New folder\Sub_1\file (1).pdf
C:\New folder\Sub_1\file (2).pdf
C:\New folder\Sub_1\file (3).pdf
C:\New folder\Sub_2\file (4).pdf
C:\New folder\Sub_2\file (5).pdf
C:\New folder\Originals\file (1).pdf
C:\New folder\Originals\file (2).pdf
Example batch file - Find all pdf's (excluding Originals)
:: Example.bat
#echo off
set myDirectory=C:\New folder
:: Search through myDirectory to find all .pdf files (including subdirectories)
setlocal enableDelayedExpansion
for /R "%myDirectory%" %%G in (*.pdf) do (
set inputDirectory=%%~dpG
echo G = !%%G!
echo inputDirectory = !inputDirectory!
for /f "delims=\" %%F in ("!inputDirectory!") do (
set currentFolder=%%~nxF
echo currentFolder = !currentFolder!
)
if NOT "!currentFolder!"=="Originals" (
set /a count=count+1
)
)
echo There are %count% PDF's (excluding originals)
pause
Please run the example batch file to demonstrate what I have so far. Any help or solutions would be appreciated.
Cheers!
Solved
Here's what I came up with based on everyone's solutions!
#echo off
setlocal enabledelayedexpansion
set count=0
set myDirectory=C:\New folder
for /r "%myDirectory%" %%i in (*.pdf) do (
set inputDirectory=%%~dpi
set inputDirectoryNoSlash=!inputDirectory:~0,-1!
for %%j in (!inputDirectoryNoSlash!) do set sub=%%~nxj
if NOT !sub!==Originals (
set /a count=count+1
)
)
echo There are %count% PDF's (excluding originals)
pause
Thanks again guys!
Another similar method is:
#echo off
setlocal enabledelayedexpansion
set count=0
set dir=C:\TEST
echo %count%
for /r "%dir%" %%i in (*.pdf) do (
set dirx=%%~dpi
set con=!dirx:~0,-1!
for %%j in (!con!) do set sub=%%~nxj
if !sub!==Originals (
cls
) else (
set /a count=count+1
)
)
echo !count!
pause
*Change to appropriate pathname
%%~pG Expand %%G to a Path only including a trailing \ backslash. Undesired, clear it away as follows:
for /f "tokens=* delims=\" %%F in ("!inputDirectory:~0,-1!") do (
set currentFolder=%%~nxF
echo currentFolder = !currentFolder!
)
Try this:
#echo off
setlocal EnableDelayedExpansion
set myDirectory=C:\New folder
set count=0
for /F "delims=" %%a in ('dir /S "%myDirectory%\*.pdf" ^| findstr /V /L "\Originals\"') do (
echo File: %%a
set /A count+=1
)
echo There are %count% PDF's (excluding originals)
pause

Change File Name to Folder Name

I've been working on a batch script (.bat) to change file name to the same folder name 3 levels up but no luck. I can only get it to rename 1 level up. There are multiple files in multiple folders.
Any help would be greatly appreciated.
I don't completely understand the intent of your current code. But the following will rename a given file based on the folder 3 levels up. Note that you can only rename one file with a given extension per folder using this strategy.
#echo off
setlocal
set "file=H:\Techs\Exported Videos\Scenario\1-CAS IED\Media player format\A8 West\abc.avi"
for %%A in ("%file%") do for %%B in ("%%~A\..\..\..") do ren "%%~A" "%%~nxB%%~xA"
Note that I include the extension of the folder name because folder names can include dots.
EDIT
Based on OP's comments, I believe the following will properly rename all relevant .avi files. The code has been tested, and works in my hands. Simply set the root and files values as appropriate.
#echo off
setlocal
set "root=H:\Techs\Exported Videos\Scenario"
set "files=*.avi"
for %%A in ("%root%\.") do set "root=%%~fA"
for /f "delims=" %%A in ('dir /b /s /a-d "%root%\%files%"') do (
for %%B in ("%%~A\..\..\..") do (
if /i "%%~dpB" equ "%root%\" ren "%%~A" "%%~nxB%%~xA"
)
)
you might try this:
#echo off &setlocal
set /a PathLevel=3
for %%a in (
"H:\Techs\Exported Videos\Scenario\1-CAS IED\Media player format\A8 West\1-CAS IED.avi"
"H:\Techs\Exported Videos\Scenario\2-SAF\Media player format\A8 PTZ\2-SAF.avi"
) do (
call:doit "%%~a"
)
goto:eof
:doit
set "fname=%~1"
set /a cnt=0
for %%a in ("%fname:\=","%") do set /a cnt+=1
set /a cnt-=PathLevel
for %%a in ("%fname:\=","%") do (
set /a cnt-=1
setlocal enabledelayedexpansion
if !cnt! equ 0 (
endlocal
set "nname=%%~a"
) else endlocal
)
echo ren "%fname%" "%nname%%~x1"
goto:eof
output is:
ren "H:\Techs\Exported Videos\Scenario\1-CAS IED\Media player format\A8 West\1-CAS IED.avi" "1-CAS IED.avi"
ren "H:\Techs\Exported Videos\Scenario\2-SAF\Media player format\A8 PTZ\2-SAF.avi" "2-SAF.avi"

Resources