Jump to content

Renaming a file into multiple different names

Navje
Go to solution Solved by minibois,
10 hours ago, Navje said:

Hey minibois,

 

thanks for reply, but somehow I do not get it right, I attached image, could you please check, what am I doing it wrong?

 

Thank you

The line I wrote I only tested in CMD. That seems to use a different syntax as compared to Powershell.

This CMD code:

FOR /F %i IN (names.txt) DO copy /y FileToCopy.txt files\%i.txt

(posted previously) in Powershell would be:

$fileToCopy = "C:\FileToCopy.txt"
$placeToCopyTo = "C:\Files\"
$namesFile = "C:\names.txt"

$fileExtension = $fileToCopy.extension

foreach($name in Get-Content $namesFile) 
{
	$pathToCopyTo = "$placeToCopyTo$name.$fileExtension"
    
    if(($name -match $regex) -And (![System.IO.File]::Exists($pathToCopyTo)))
    {
    	Copy-Item $fileToCopy -Destination $placeToCopyTo$name.$fileExtension
    }
}

I added some more stuff to this script so there is less work for you to do.

You only have to tell the program:

- The file you want to copy (line 1)

- The place you want to copy it to (leave empty if you want it to copy in the folder the Powershell is currently in with cd) (line 2)

- The names file (line 3)

 

It also checks if the path you're trying to write to exists already. If the file name already exists, it just doesn't copy.

i.e. if you have "exampleA" in your names.txt file, but already have

 

Depending on your usage, you could consider saving the names of the files it couldn't make. The reason it couldn't create a file is because there was already a file with that name, but an older file; thus you might want a newer version or something..

That depends on your workflow.

 

Hope this code helps!

Hi,

 

I'll simplify question, I have one file that needs to be renamed and copied into multiple different names is there any batch in windows 10 that I could use, I am also opened for any other suggestions?

Link to comment
Share on other sites

Link to post
Share on other sites

-thread moved to programming subforum-

 

Yes any script of your choosing should be able to do that. Personally I'd use C++.  It's completely overkill but I know it well enough that it would be trivial, and the same cannot be said for my skills with a .bat file.

Solve your own audio issues  |  First Steps with RPi 3  |  Humidity & Condensation  |  Sleep & Hibernation  |  Overclocking RAM  |  Making Backups  |  Displays  |  4K / 8K / 16K / etc.  |  Do I need 80+ Platinum?

If you can read this you're using the wrong theme.  You can change it at the bottom.

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Navje said:

I have one file that needs to be renamed and copied into multiple different names

here is one way, if I understand your question correctly..

- copy & paste manually for required number of files

- get file list into excel (manually drag down from excel or use dir / .bat for file list)

- manage the new names according to the existing list , then get a fomula :

="ren"[space]"old file name with extension file type"[space]"new name & format"

drag down in excel to complete the list.

- copy the list into notpad & save as .bat, all file types . put the .bat file into the folder & run

Link to comment
Share on other sites

Link to post
Share on other sites

59 minutes ago, dgsddfgdfhgs said:

- copy & paste manually for required number of files

 

Hey thanks for reply,

 

well that is the step that i wanna avoid it.

 

lets say like that, I have file with name "B-45963.eps" and i have list of names that this file needs to be renamed, file extension stays same.

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Ryan_Vickers said:

Personally I'd use C++

sorry my programing skills are less less than 0 :)

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Navje said:

lets say like that, I have file with name "B-45963.eps"

hmm..i dont find it an issue, 

if you manually copy, it becomes

B-45963.eps

B-45963 - Copy (1).eps

B-45963 - Copy (2).eps

...etc

which you shall easily control no. of files and manage filelist

 

and if you are talking about thousand of them, still copy & paste manually so that it multiplies by 2 each time, and use bat function :

 

dir /b > fileslist.txt

execute in the working folder & trans to excel for further works

 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

This should do that, with some personal changes:

FOR /F %i IN (names.txt) DO copy /y FileToCopy.txt files\%i.txt

Replace FileToCopy.txt with the file you want to copy (you can either cd to the directory, or place an absolute link there, like this:

"C:\Users\MyUsers\MyFiles\File.txt"

Also be sure to change the extension type, I just placed .txt there to try it out..

 

In this example, I made a .txt file with names (names.txt) and I file I want to copy (FileToCopy.txt).

What this code will do, is for every line in names.txt it will copy FileToCopy and rename it to whatever that line in names.txt is.

 

Input:

image.png.8037cfdccdc7a374e8c9b274a342737c.png

 

Output:

image.png.e9bf517445020c76e2aa91776bc8905f.png

 

If you want to make it so it automatically grabs the file extension type... I can probably look into how that works.

Be sure to test these sorts of scripts in a different folders - so you don't accidentally add a ton of new files/folders to another place you didn't mean to have them..

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

33 minutes ago, minibois said:

FOR /F %i IN (names.txt) DO copy /y FileToCopy.txt files\%i.txt

Hey minibois,

 

thanks for reply, but somehow I do not get it right, I attached image, could you please check, what am I doing it wrong?

 

Thank you

idk.jpg

Link to comment
Share on other sites

Link to post
Share on other sites

10 hours ago, Navje said:

Hey minibois,

 

thanks for reply, but somehow I do not get it right, I attached image, could you please check, what am I doing it wrong?

 

Thank you

The line I wrote I only tested in CMD. That seems to use a different syntax as compared to Powershell.

This CMD code:

FOR /F %i IN (names.txt) DO copy /y FileToCopy.txt files\%i.txt

(posted previously) in Powershell would be:

$fileToCopy = "C:\FileToCopy.txt"
$placeToCopyTo = "C:\Files\"
$namesFile = "C:\names.txt"

$fileExtension = $fileToCopy.extension

foreach($name in Get-Content $namesFile) 
{
	$pathToCopyTo = "$placeToCopyTo$name.$fileExtension"
    
    if(($name -match $regex) -And (![System.IO.File]::Exists($pathToCopyTo)))
    {
    	Copy-Item $fileToCopy -Destination $placeToCopyTo$name.$fileExtension
    }
}

I added some more stuff to this script so there is less work for you to do.

You only have to tell the program:

- The file you want to copy (line 1)

- The place you want to copy it to (leave empty if you want it to copy in the folder the Powershell is currently in with cd) (line 2)

- The names file (line 3)

 

It also checks if the path you're trying to write to exists already. If the file name already exists, it just doesn't copy.

i.e. if you have "exampleA" in your names.txt file, but already have

 

Depending on your usage, you could consider saving the names of the files it couldn't make. The reason it couldn't create a file is because there was already a file with that name, but an older file; thus you might want a newer version or something..

That depends on your workflow.

 

Hope this code helps!

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

You can write your own batch file in Notepad++ without any confusing commands.

 

You want to create the command  COPY  original_filename.extension  destination_filename.extension  on each line, with both file names between "  " , which are needed if the file names have spaces in them

 

Open a document in Notepad++ , type your file names. Leave an empty line at top and bottom:

Now, open Search > Replace , make sure Extended is checked near the bottom and then you'd want to do TWO search and replace operations

 

1. Replace \n   with \nCOPY "Your file name.extension" "  (no space after last quote)

This means  look for every new line character (end of the line) and replace it with another new line, followed by that text. You're adding the COPY command, then your original file name between quotes, then a space followed by the quotes before the destination filename.

 

image.png.7631ce3e95de09d5522fe6fb660a03bf.png

 

Hit "Replace all" and the result will be this:

 

image.png.e1e6c7aceb944d442d6e5c442fb1fc16.png

 

 

2.  Now you want to put a quote at the end of each line and the extension if needed, so you do Replace  \r  with  .extension"\r 

(optionally you can remove the extension and use just "\r)

 

Why \r ?  Because the Enter key in Windows is actually made out of two characters,  \r  and \n - carriage return (go back to beginning of line) and new line (advance one line down). So, you want to put the quote character before the first character of the new line combination.

If you do this in Linux or Mac, they usually use only the \n character, so you can use \n instead or \r

 

image.png.78707755b4694220bd4e8c17a326278d.png

 

 

Hit Replace all and this is your result:

 

image.png.30c87200207bca93533aaecac3fe7e18.png

 

 

Now you can simply delete the first and last lines (as they're trash) and save the file with the .BAT extension in the same folder where your original fileis and then run the file by double clicking it.

 

Link to comment
Share on other sites

Link to post
Share on other sites

56 minutes ago, minibois said:

The line I wrote I only tested in CMD. That seems to use a different syntax as compared to Powershell.

This CMD code:


FOR /F %i IN (names.txt) DO copy /y FileToCopy.txt files\%i.txt

(posted previously) in Powershell would be:


$fileToCopy = "C:\FileToCopy.txt"
$fileExtension = $fileToCopy.extension

$placeToCopyTo = "C:\Files\"

$namesFile = "C:\names.txt"

foreach($name in Get-Content $namesFile) 
{
	$pathToCopyTo = "$placeToCopyTo$name.$fileExtension"
    if(($name -match $regex) -And (![System.IO.File]::Exists($pathToCopyTo)))
    {
    	Copy-Item $fileToCopy -Destination $placeToCopyTo$name.$fileExtension
    }
}

I added some more stuff to this script so there is less work for you to do.

You only have to tell the program:

- The file you want to copy (line 1)

- The place you want to copy it to (leave empty if you want it to copy in the folder the Powershell is currently in with cd)

- The names file

 

It also checks if the path you're trying to write to exists already. If the file name already exists, it just doesn't copy.

i.e. if you have "exampleA" in your names.txt file, but already have "exampleA.txt" in the folder, it just doesn't copy it.

Not sure what you exact usage is, but you could consider saving all the names it couldn't make a file for. That could for example be because there is an old file already? Not sure what you workflow looks like..

 

Hope this code helps!

 

THANK YOU million times! 

If you ever come into our god forsaken country called Slovenia, give me a nudge and we'll grab some beer.

 

Thank you m8!

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×