How to write a bat file to rename files in file hierarchy - batch-file

I'm have a lot of pictures in a file system. I'm trying to figure out a way to rename all of the files, and if possible put them in one folder. At the very least rename them where they are.
They are currently in a hierarchy like this:
folder1\folder2\filename.jpg
Is there any way I can write a script to rename the files so the name of the actual file would be:
folder1-folder2-filename.jpg
I'm not sure how to go about this. Any suggestions or a push in the right direction would be greatly appreciated.

try this..
for %%* in (.) do set name=%%~pn*
set name=%name:\=-%
set name=%name:~1%
%name% now has the folder name. add this to your file name.

Related

copy all files from one folder to a destination folder name with a pattern match

I'm trying to work out how to create a batch file to copy files from a folder on a network share (such as D:\folders\folderA) to a local folder on my C: drive that matches a specific naming convention (such as C:\Users\john\folderddmmyy0600). In my case I only want to copy the files to a folder that matches something like folder1202210600 so is it possible to use a regex match to accomplish this?
For example, would something like this work?
copy "D:\folders\folderA" *.* "C:\Users\john\folder\d+"
My batch file writing is not good, but would appreciate if I can get some opinions on this.
Thank you
Will look into the PowerShell option Compo, thank you.

Batch file to move a number of files, then replace each of them with one file of another type while letting them keep their original name

First off I want to say that
-I didnt ever create a batch file yet, but I am really willing to learn
-I am not even sure if what i want to do is possible with a batch file
What i want to do is the following:
I want to replace a number of files of one file type in a folder each with one and the same file of another file type. In doing this, i want the "replaced" files to keep their original name except for the "replacer" file's extension. I am not talking about file conversion, this is about replacing several different files each with one and the same file, so each of them will look the same later, just with different names and the file extension of the "replacer" file. All of the files inside the folder are to be treated this way, there are no exceptions.
So it looks something like this:
Folder 1 Folder 2
10000000.tga------------->10000000.png (looks like replacer.png)
10000001.tga------------->10000001.png (looks like replacer.png)
10000011.tga------------->10000011.png (looks like replacer.png)
I really hope that my description is sufficiently precise, if not so, I am of course willing to give any information needed. I found parts of what i need (e.g. a loop for files in a folder, an order to replace one file with another file) but I am unsure of how to combine them at all, let alone to achieve what I actually wanted to do.
Any help is greatly appreciated :)
for %%i in (*.tga) do (
copy "replacer.png" "%%~ni.png"
del "%%i"
)
see for /? for details about the %%~.. syntax

Batch file to transfer files with certain phrase in name

I currently use a batch code which transfers files from one place to another. I wish to elaborate on that code to only transfer files which have a certain phrase in their file name.
I did not write the code, and I do not fully understand it but I know what is does.
REM choose desired drive
cd \
Z:
REM change to required directory
cd out
REM cpy all files using * to the desired directory
copy *.dat \\server\f\rug_data\received_transfer
REM delete all files in the folder
REM del *.usr
All I want to do now is add a bit which says only transfer files with 'D0036' in their name.
I have spent time googling but could not find exactly what I was after.
Any help greatly appreciated.
change
copy *.dat \\server\f\rug_data\received_transfer
to
copy *D0036* \\server\f\rug_data\received_transfer
OR, if you mean "All .dat files with D0036 in their name"
copy *D0036*.dat \\server\f\rug_data\received_transfer
* means "match any number of any characters"
Like this?
copy *D0036*.dat \\server\f\rug_data\received_transfer
This works like a charm!
move /-y "C:\(Folder that the contains files)*(Specify a certain character)*" "C:\(Dest Folder)"
This will move everything in the folder with the name that you wrote.
Hope it works!
Logan

Hey, i need help in batch to make it copy everything in 1 folder to another folder

im working on a script that have few folders and i don't really understand how to make an config for this but i don't need it.
i have setup a script config inside
Set cfg=cfg2
Set txt=txt2
Set rar=rar2
cfg2, txt2, rar2 are in a diffrent directory
the files in cfg are cfg files but theres alot of them.
this also goes for txt & rar too.
i want a copy script that copies all cfg files (and only cfg files) into the cfg2 file that are in another directory.
and then this goes again for the txt & rar files.
I also know the code for move but i really want to copy it
cause on move you just do
move "txt/*.txt" %txt% and thats kinda what i want to do but that aint working
Have a look at robocopy from microsoft.
Something like the below should work;
robocopy /s \path\cfg \path\cfg2 *.cfg

.bat file to rename and move files with prompt

I am completely new to this, but I am trying to create a .bat file that will allow me to rename a pair of files within a designated folder and move them into a subfolder. The part I am having trouble with is that I am wanting a prompt to come up to identify/select the files to be renamed and moved.
Example file names are:
A1234, A1235, A1236, B1234, B1235, B1236, etc.
Is there a way to bring up a prompt that allows the user to type the shared name (ex 1234)of the files and rename and move both files to the designated subfolder?
Any and all help would be appreciated!
Suggested approach
for part of problem
part I am having trouble with is that I am wanting a prompt to come
up to identify/select the files to be renamed and moved. Is there a
way to bring up a prompt that allows the user to type the shared name
(ex 1234)of the files and rename and move both files to the designated
subfolder?
Do a search operation using wildcard, like "?1234" for the case highlighted above ( should be made generalized for all acceptable and expected patterns "*1234*" is the generic most )
Now do a RENAME inside a For loop on the results obtained by search.
As you suggest you are a newbie with Batch, following tutorials will help you build your file. Look for elements like Variables, For Loop
Batch Tutorial
Here you go
#echo off
set /p file=Please type shared name:
for %%a in (C:\Folder\?%file%.*) do (
move "%%a" subdir
ren "subdir\%%a" newname.*
)

Resources