How Can I Batch Rename Files in a Folder? - file

I have a folder with hundreds of files.
I need to rename the files in this folder one by one; but this takes a lot of time.
I did some research and found that it can export names from a text file to files in any folder I want.
But the videos contained insufficient information for me to implement it.
What I've been able to do so far:
I was able to transfer file names to TXT file with CMD command.
Note: After exporting the filenames I found some errors in the sorting.
You can see the related error in the "Example Files II" section.
Example files I:
Files and their real names.
Example files II:
I exported the file names to txt file with CMD; but the order of the fil names is not the same as in the TXT file with slight differences.
Note: This is not a big problem for now; but I would like to know if there is a way to do the correct sorting.
Export File Names
Example files III:
I batch corrected the filenames with EmEditor.
Now I need to automatically replace these names with the files in the folder.
Here I don't know how to do this.
If anyone can provide practical information on this subject, I would be glad.
Corredted File Names

You can use Powershell and a regular expression to rename files. Go to the folder in Explorer, select Open Windows Powershell on the File menu, and enter the following:
Get-ChildItem *.zip | Rename-Item -NewName { $_.Name -replace '-[0-9]{4}-[0-9]{2}-[0-9]{2}(-[0-9]{2}-[0-9]{2}-[0-9]{2}\-utc)?\.zip$','.zip' }
References:
Use Regex / Powershell to rename files
Replacement operator

Related

Tricentis Tosca - How to open a file which is the first file in a folder, or the only file in a folder. Note - I dont know the file name

The System Under Test generates some pdf files and saves the files to a particular folder. The challenge is that the pdf file name is randomly generated and I don't know the name. I need to open the file from the folder which is the most recent one, but I can't seem to find a way to open the file.
I know part of the file name so I tried using wildcard for part of the file name, but it doesn't work
I removed all other files from the folder and kept only one file in the folder, and then used wildcard instead of the complete file name, but that doesn't work either
Is there a way that I can read names of files present in a folder? Or open a pdf file using wildcard character? Or open the first file in the folder?
You could try PowerShell scripting (please refer attached screenshots)
Use the 'TBox Start Program' module.
In the Path attribute, mention the complete path of your PowerShell exe file (Eg: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe)
In the Directory attribute, mention the folder path(Eg: C:\Users\tosca\Documents\testfolder)
Put -command in arguments with ActionMode 'Select'.
As Argument, input the PowerShell script with your wild carded file name to copy the file name (Eg:
(Get-ChildItem filename | Sort-Object CreationTime -Descending | Select-Object -First 1).Name | Clip)
Copy latest file name
Then use 'TBox Clipboard' to store the dynamic file name using ActionMode 'Verify'
Save latest file name
Then you can buffer the target file as folderpath\filename.

Stata how to export delimited files using 'file` in a loop

I'm importing dta files in a folder and exporting each to a csv file. I'm not sure why but the loop doesn't save the file. Here's the code:
global path "file path"
cd "${path}"
* Geocodes for edd
clear
local files: dir "${path}Data\MeasureData\" files "*_edd.dta"
*do loop to bridge file for EDD and
foreach file in `files' {
use "${path}Data/MeasureData/`file'", clear
rename beafips edd_id
merge m:1 edd_id using Data\TempData\bridge_edd.dta
keep if _merge==3
drop _merge
export delimited "${path}Data\OutgoingData\`file'.csv", replace
}
I keep getting error like this:
file filepath\Data\OutgoingData.csv saved
I was expecting this to be saved as filepath\Data`file'.csv. What did I do wrong?
This is happening because Windows defaults to using backslashes in path directories, but in this context your computer is reading the backslash as an escape character and so isn't interpreting the ` as indicating the beginning of your local files.
This problem won't typically appear on a Mac/Linux machine as they default to using forward slashes in directory paths.
So the solution is to change all the \s to /s in your code. See here for a more detailed write-up of the problem: https://journals.sagepub.com/doi/pdf/10.1177/1536867X0800800310

Winrar, "-or" overwrites file in archive instead of adding number to name if exists

After Googling and "researching" for over 3 hours to no avail, i have turned here to possibly get some help to my issue. Either if its possible or not to some extent.
This is a bit messy i know, but i'm fairly new to this.
What im trying to do.
I want to Backup a File which adds it to a archive folder With a Timestamp to it. YYYY-MM-DD Which then contains the File
2021-03-09.rar
File01.db
Then it takes the created file and adds it to a Backup.rar
Backup.rar
2021-03-09.rar
File01.db
Now i got this to work, My only problem here is that it instead overwrites the current existing File "2021-03-09.rar" in the Archive "Backup.rar" with the new one, instead of adding a number to the new backup that i added.
This is how i want/it should do;
Backup01: 2021-03-09.rar
Backup02: 2021-03-09-01.rar
Backup03: 2021-03-09-02.rar
Backup04: 2021-03-09-03.rar > and so on.
Code/what i have tried
I have tried using the commands "M" and "U" but same result, i tried using -or as well, which should rename files automatically if a file with the same name exists, but it doesnt work and the file(2021-03-09.rar) in my archive(Backup.rar) gets overwritten instead with the new backup(2021-03-09.rar).
Code:
"%ProgramFiles%\WinRAR\rar.exe" a -agYYYY-MM-DD -cfg -ep1 -m5 -r -y "C:\Users\%UserProfile%\Documents\0-Background\BAT FILES\Database Backups\Backups\.rar" "C:\Users\%UserProfile%\Documents\0-Background\BAT FILES\Database Backups\File01db"
"%ProgramFiles%\WinRAR\rar.exe" a -or -ep "C:\Users\%UserProfile%\Documents\0-Background\BAT FILES\Database Backups\Backups.rar" "C:\Users\%UserProfile%\Documents\0-Background\BAT FILES\Database Backups\Backups\*"
While i know its not the smartest way to add a rar file to another rar file, its what i decided to do, it might be smarter to just have the bat file create a folder that it then adds to the archive or if it creates a folder in winrar and then adds the file to the folder in the archie, but it seemed to advanced.
Use YYYYMMDD and add an archive number, where the archive number is a numeric value starting from 1.
That will generate unique names even when the YYYY-MM-DD-NN format mask is used more than once in the same day.
WinRAR a -agYYYY-MM-DD-NN backup
WinRAR searches for already existing archives with that generated name and if found, increments the archive number to generate an unused name.
Refer to WinRAR, Help. Help topics and search for '-ag'

Replacing files in a *.zip file without unzipping (extracting) in Powershell

Hi everyone!
Currently, I am trying to create a script in Powershell that will replace/delete files in a *.zip file without extracting it.
I know it is possible to do manually by using applications like 7Zip and WinRAR or by simply opening a zip archive in Folder Explorer, but I am looking for a way to automate this action.
I am new to powershell and to programming in general, so I cannot come up with any preliminary code. I tried to search for answers on the net, but failed to find anything that would work for me.
So the question is:
Lets say we have a zip archive called Photos (Photos.zip). The archive contains 5 images. How do I replace/delete images in Photos.zip without extracting it in Powershell?
Any code/ideas/links to helpful resources would be highly appreciated.
I used the following solution.
First I moved old files from the archive to a temporary folder by using this code:
#Path to the temporary folder
$pathToTemporaryFolder = "C:\...\Temporary"
#Path to a file that will be moved from the archive to the temporary folder
$pathToFileToBeMoved = "C:\...\Photos.zip\My Photos\My Image.png"
(New-Object -COM Shell.Application).NameSpace("$pathToTemporaryFolder").MoveHere("$pathToFileToBeMoved")
Then, I copied some new files to the archive:
#Path to the folder with a new file
$pathToFolderWithNewFile = "C:\...\New Images\My New Image.jpeg"
#Path to a folder in the archive where the new file will be copied to
$pathToFolderInArchive = "C:\...\Photos.zip\My Photos"
(New-Object -COM Shell.Application).NameSpace("$pathToFolderInArchive").CopyHere("$pathToFolderWithNewFile")
Finally I deleted the temporary folder
Remove-Item -Path "C:\...\Temporary" -Recurse
This is how I deleted files from the archive and copied new files to it without unzipping/extracting.

How to copy a freshly created array containing files to another folder on the same computer

I am new to Powershell but have had experience with various programming languages including dBase III, basic, Fortran (shows my age). I am a musician and launch MP3 files as an accompanyment when I perform live, somewhat like karaoke. I have about 45 different MP3s that I use. I like to change the sequence of the songs that I perform, from time to time and this requires renaming the MP3 file names with a preceeding sequence number. The file names take on the form: "01 songnameA.mp3", "02 songnameB.mp3", "03 songnameC.mp3", etc. I also repeat songs so there might be an "09 songnameA.mp3" in the folder that I play back from. To do this, I typically create a text file that lists the songs in the order that I want to perform them. Each line in the text file has the form "01 Songname X" but no MP3 extension. I then manually copy the mp3 files into a folder and then edit the names applying a sequence number according to the text file. This is time-consuming.
I have created a Powershell script (version 2) that creates an array of the text file content and an array of the unnumbered MP3 song files. The script creates a 3rd array containing numbered MP3 filenames according to the sequence in the text file. This array does work and I can easily display the list of items in it which have the form "01 SongnameX.mp3". However, I have been unable to copy this array of MP3 filenames with a preceeding sequence number into another folder. I don't know how many variations of the "Copy-Item" statements I have tried but nothing works. The name of the array that contains the filenames is $nsfarray (new song file array). The command I am presently using:
$nsfarray | copy-item -Destination C:\temp
Returns the following error message:
Copy-item : Cannot find path 'C:\Users\My HP\Documents\My Scripts\01 A Good Time.mp3' because it does not exist.
The path is the default path that I use to run Powershell but somehow the MP3 file names get appended to it. The "01 A Good Time.mp3" is the first item in the $nsfarray. I know I am missing something here. Any suggestions would be appreciated.
This is really crude but the issue I see with your input file is that it obviously does not exactly match the real file name that you are looking to copy. So with that in mind lets try this on for size.
$musicPath = "c:\music"
$destinationPath = "c:\songset"
$nsfarray = "01 Age of Aquarius"
$nsfarray | ForEach-Object{
If($_ -match '^\d+ *(?<BaseName>.*)'){
Copy-Item "$musicPath\$($Matches.BaseName).mp3" -Destination "$_.mp3"
}
}
Work with explicit paths: $musicPath and $destinationPath so that we do not have to rely on our current location in PowerShell. Then we navigate each value in the array, which I have populated with one example. Now we need to extract the real file name away from your set list number. Using regex we return the part of the string that is not the beginning numbers and spaces and take everything after that.
After we simply copy the file that exists in the $musicPath folder that has the name "Age of Aquarius.mp3", which should be the real file, and copy it to the $destinationPath as "01 Age of Aquarius.mp3", which is how you want the file for your gig.
Clarification
After answering this and looking back at the question I think there is a chance I didn't understand it right. Please update the question if this is not the right way to address this. The core of your problem, if nothing else, is that you are not specifying a proper file path for the mp3s. You need to use the proper source folder and append the mp3 to the string.

Resources