How to use batch file to append text to filename? - batch-file

I've created a batch file to move files that are over 30 days old to an archive folder. I would also like to append a string of text to the end of the files that are moved. Here is what I have for moving the files older than 30 days:
robocopy "C:\...\Data" "C:\...\Archives" /move /minage:30
That part is working great. Any advice on the best way to add a string of text such as "XYZ" to the end? I.E. datafile_XYZ.txt
Thank you in advance for any help!

This could be accomplished with a one-line PowerShell command:
powershell "Get-ChildItem C:\...\Archives\*.* | Where-Object {$_.Name -notlike '"*_XYZ.*'"} | Foreach-object {Rename-Item -Path $_.FullName -NewName ($_.name -replace '\.', '_XYZ.')}"

forfiles /d -30 /m *txt "cmd /c ren #file #fname_xyz.#ext"
more info - http://ss64.com/nt/forfiles.html

Related

Batch file that will delete files that are older than 6 months, but will exclude files listed in a separate text file

Just wanted to ask for help in batch file. I need to have a text file where the file names of excluded files would be stored in the deletion of files with 6 months above date.
Inside list.txt are trial.csv, trial2.csv which are older than 6 months but should be excluded in the deletion since it is in my list.
I have this code but can't seem to make it work. Please help
#echo off
set exclude_file=C:\Users\Desktop\Test\list.txt
set directory=C:\Users\Desktop\Test
forfiles /p %directory% /s /m *.* /c "cmd /c if #fdate < %date% -180 and not #isdir == true (find /i ""#fname"" < %exclude_file% >nul || del /q #path)"
pause
If you are okay with using Powershell, try this. Would recommend using a -whatif on the remove-item line for testing. Use at your own risk...
$protectedFiles = Get-Content -Path "C:\Users\Desktop\Test\list.txt" | Where {$_}
$oldFiles = Get-ChildItem -Path "C:\Users\Desktop\Test" -File -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt (Get-Date.AddMonths(-6)}
Foreach($oldFile in $oldFiles){
If ($protectedFiles -NotContains $($_.Name)){Remove-Item $_ -Force}
}

batch script to rename the most recent file in all subfolders?

#echo off
for /f %%i in ('dir /b /a-d /od /t:c "*.pdf"') do (
set LastFileCreated=%%i
)
echo The Last File Created is %LastFileCreated%
pause
:: ren %LastFileCreated% "decs365.pdf"
pause
all I have is this script, but unfortunately it needs to be copied in each and every sub folder, thus defeating the purpose, and it wont even rename the files. Is there any way to add a specified path "D:\Monthly Report" and have it rename the most recently created file in all of the sub folders to "decs365"
I'd suggest you leverage powershell.exe, from your batch-file for this task:
#%__AppDir__%WindowsPowerShell\v1.0\powershell.exe -NoProfile "Get-ChildItem -Filter '*.pdf' -File -Recurse | Sort-Object CreationTime -Descending | Select-Object -First 1 | Rename-Item -NewName 'Decs365.pdf'"
Obviously, if you're not running the command in the same working directory, as you are in your question, you'd need to add that directroy to the command above, e.g. Get-ChildItem -Path 'C:\MyDirectoryPath' -Filter …

Remove a file from folder using changeable path folder

I want to remove folder that existe in many project in the same workspace folder like below :
Workspace :
/project1/generated
/project2/generated
I tried the code below,but deosn't work :
del /q "workspace\(*)\generated\*"
FOR /D %%p IN ("workspace\(*)\generated\*.*") DO rmdir "%%p" /s /q
Please can someone help ?
Thanks in advance .
If you are deleting the directory (and all files it contains), then the following will work. When you are confident that the correct directories will be deleted, delete the -WhatIf from the Remove-Item command.
Get-ChildItem -Directory -Path 'C:\workspace\project*\generated' | Remove-Item -Recurse -WhatIf
If you must do it from a cmd.exe shell or .bat script, you could:
powershell -NoLogo -NoProfile -Command ^
"Get-ChildItem -Directory -Path 'C:\workspace\project*\generated' | " ^
"Remove-Item -Recurse -WhatIf"

xCopy into the top folder of a directory

I running an xcopy command to transfer from one file to the other.
xcopy /s "c:\users\documents\thisfile.txt" "d:\otherfiles\1.2.1"
I'd like to be able to just copy the file into the most recent folder in the otherfiles directory rather than hard coding it every time a new version folder is created. These are versions numbers and these tend to just increase.
Is this entirely possible?
If you wanted to do this in PowerShell, it is possible. This would require PowerShell 3.0 or higher. It can be done with 2.0, but would require changes. Hopefully, you are on or can upgrade to a modern-day version of PowerShell.
When you are confident that the file will be copied correctly, remove the -WhatIf from the Copy-Item cmdlet.
$fn = 'C:/src/t/xxx.txt'
$destbasedir = 'C:/src/t/lastdir'
Get-ChildItem -Directory -Path $destbasedir |
Sort-Object -Property Name |
Select-Object -Last 1 |
ForEach-Object { Copy-Item -Path $fn -Destination $_.FullName -Whatif }
This could be put into a .bat file script.
SET "FN=C:\src\t\xxx.txt"
SET "DESTBASEDIR=C:\src\t\lastdir"
powershell -NoProfile -Command ^
"Get-ChildItem -Directory -Path %DESTBASEDIR% |" ^
"Sort-Object -Property Name |" ^
"Select-Object -Last 1 |" ^
"ForEach-Object { Copy-Item -Path "%FN%" -Destination "$_.FullName" -Whatif }"
Ok, it is possible to check the versions of the directories, but that will take a bit more code as we cannot simply remove the dots to get a numeric value and compare to the next. The reason being, considering versions 1.2.3 and 1.23 if we remove the dots to make it a matchable numeric value, both these will end up being being 123 therefore each version section would need to be tested.
However, based on your comments to my questions, you create new versions as folders, and therefor it ia sortable by date, so simply run a dir command and sort by created date. It will set the latest folder as the variable you need:
#echo off
for /f "delims=" %%i in ('dir /b /ad /o:d D:\otherfiles') do set "myvar=%%i"
xcopy /s "c:\users\documents\thisfile.txt" "d:\otherfiles\%myvar%"

Move .txt files in different folders to a single folder using Batch file

I'm trying to move all .txt files in different folders to a single folder using a batch file, I'm new to batch coding so I'm having some difficulties.
My code is as follows:
FOR /D /r %%G IN ("C:\Users\Rodrigo\Desktop\PR\2016\08.2016\") DO MOVE G\*.txt C:\Users\Rodrigo\Desktop\PR\2016\
See the correct syntax of For /r or in an open cmd window type help for
#Echo off
For /r "C:\Users\Rodrigo\Desktop\PR\2016\08.2016\" %%G IN (*.txt
) Do echo Move "%%G" "C:\Users\Rodrigo\Desktop\PR\2016\"
Pause
If the output to screen looks OK remove the echo in front of the move command.
You could do this all in PowerShell. I am not sure it will work if you do not pass the $_.FullName which contains the path to the file.
Get-ChildItem -Path "C:\Users\Rodrigo\Desktop\PR\2016\08.2016\" -Filter *.txt | `
ForEach-Object { $_.FullName } | `
Move-Item -Destination "C:\Users\Rodrigo\Desktop\PR\2016\"

Resources