Renaming file paths in batch files - batch-file

I need to execute several rename commands in a batch file and wish to store the batch file in a directory different from the directory storing the files that need to be renamed.
The following is a sample file
ren C:\test\old1.txt new1.txt
ren C:\test\old2.txt new2.txt
I would ideally like to not have to copy the directory path everytime, since I might need to change the folder in which these files reside. I tried the following but it does not work. Probably I am not understanding the set command correctly.
set dirpath=C:\test\
ren %dirpath%old1.txt new1.txt
ren %dirpath%old2.txt new2.txt
Any ideas how I might achieve this effect.

Your batch file seems to be correct. Just to make it play well with path names that contain embedded spaces, put double quotes around the name, like so:
set dirpath=C:\test\
ren "%dirpath%old.txt" new.txt
Obviously, if you have a hardcoded path (C:\test\) you shouldn't even need that. I tried your batch file locally and worked fine.

Related

Delete duplicate files of different extensions recursively

I'm trying to create a batch file that can delete .xmlb files is there is a .engb file of the same name in the same folder. I want it to work so that it can check the folder where the batch file is placed as well as all of the sub-folders. I found this question where someone pointed out how to do this in a single folder, but I'm trying to do it in every sub folder. Here's what I have so far:
#echo off
for /r %%I in (*.xmlb) do (
if exist %%~pnI.engb del %%I
)
pause
It gives me a "the system cannot find the path specified" error. I tried changing del with echo, and when I do that it does manage to display the names of the .xmlb files (with .engb extensions), so I'm assuming it can actually find them and my syntax is off somewhere. If I try to use %%~nI like the original code, it won't do anything (because the .engb files are in the same folders as the .xmlb files). I'm sure I'm probably missing some small thing that's preventing it from deleting the right files. Any help is appreciated! I'm attaching two screenshots: one with the batch file in a folder, and then one of the sub-folders that contains .xmlb and .engb files. So in the jungle1, jungle2, and jungle3 folders I want to keep the .engb files and remove any .xmlb files with the same names as .engb files

batch file which looks for specific files

I'm wondering if a batch file could help to move all the files which i need from a couple of folders to a specific one. The folder structure looks like this example below:
d:\home\\(random)\upload\\*.*
d:\home\\(random)\uplaod\\*.*
The files in the directory upload should be moved to a specific folder. It can be done by putting every single path in to the batch file, (there are more then 100 folders and there will some more in the future). I guess there is an easier way to get this done.
Thanks for you help in advance.
for /d %%a in ("D:\home\*") do echo move "%%a\upload\*" "x:\destination\
This is the syntax for use in a batchfile. For use on commandline replace every %%a with %a.
The for /d returns subfolders of D:\home\. Just append the upload\* part.

Set docx. properties using .bat

I need to create the same word documents basically every day to create reports. I would very much like to automate this using batch-script (or any script will do, really, as long as it also creates folders).
I have a .bat that creates the set of folders I need that looks like so:
#echo off
set /p dname= Enter Directory Name?
md %dname%
md %dname%
md %dname%\docx
md %dname%\xls
md %dname%\ppt
Now I need to copypaste a file to the docx folder that I have in the root folder, called intial.docx. This file needs to be renamed into
Initial_[Incrementing number]_V1_[Title].docx.
And I also need to set file properties of those files, namely Subject and Comments.
How do I do all that?
Well for the copy paste and renaming section, you could use the copy and rename commands:
#echo off
copy "directory\path\filename.docx" "directory\newpath\filename.docx"
rename "directory\newpath\filename.docx">"directory\newpath\newfilename.docx"
pause
That should work, let me know if you have any problems :)
For the properties, I guess you could just enter those into a text document,that's what I do.

.bat coding or some app maybe?

how can I organize my directories automatically?, I have a "downloads" folder, which currently contains lots of different info, for example, work related info, tv-shows, movies, etc, software, etc.
How can I automatically, maybe using some .bat execution, not sure, check for example the name of the files, or the type, and put them in the right subfolders?.
Thanks!.
You can use the move command to move files. You can also use wildcards in it.
So you could write a batch script that looks something like this:
cd C:\Users\You\Downloads
rem Excel sheets are work.
move *.xls Work
rem Reports are work.
move Report*.pdf Work\Reports
rem Pron and other viewing material ;)
move *.mp4 Private
You could run this script automatically by making it a scheduled job. Note that this script changes to the right directory first and then moves items to a relative subdirectory. This means that the directories Work, Work\Reports and Private must exist in the Downloads directory.
Of course you can expand the script to make it check and create directories as well, or you can specify different paths if you want to move the files out of the Downloads directory.
Basically, try to do it on the command line and then try to generalize those steps into your script.
This batch file will create a set of folders like .mkv .jpg .mp3 using the filetypes inside the folder, and move the files into the appropriate folders.
Launch it from your desktop and change the "c:\media" to the right folder name.
#echo off
cd /d "c:\media" && for %%a in (*) do md "%%~xa" 2>nul & move "%%a" "%%~xa" >nul

Recursively move all files to one-upper directory

So here are the questions:
I have a folder, let's say C:\myFolder, and in this directory, I have many subfolders, and in each of this subfolders, I have exactly one folder, that contains pdf files, so my file structure looks something like this: C:\myFolder\someFolderInMyFolder\theOnlyFolderInThisFolder\*.pdf, now I want to move all these pdfs one level up, such that it will be like this: C:\myFolder\someFolderInMyFolder\*.pdf. Are there any command line commands, or scripts (that can be executed by Cygwin) that will help me with this?
What could complicate the situation is that, I have manually move some files one level up by myself, so it will help if there is a check condition.
I have some .zip files that the name are generated by computers, in the format of mm/dd/yy/fileIndex.zip, and the fileIndex is like No.001, for example. When I upload the extracted folders to Dropbox, and view the files on my iPad, it looks weird because the full folder name can not be displayed completely, so I want to rename each folder to someIndex, in the above example, from No.001 to 001, so same question here: any command or shell scripts?
You can move all PDFs up one level with a slightly modified version of what #Endoro suggested:
#echo off
for /r "C:\myFolder" %%f in (*.pdf) do move "%%~ff" "%%~dpi.."
However, there is no generic way for the script to distinguish files you already moved from files that have yet to be moved. It might be best if you undid your manual moves. Otherwise you'll have to find some distinguishing feature or check each name against a list of names, e.g. like this.
You can rename files like this:
#echo off
setlocal EnableDelayedExpansion
for /r "C:\myFolder" %%f in (No.*.zip) do (
set name=%%~nxf
set name=!name:No.=!
ren "%%~ff" "!name!"
)
endlocal
FTR, I somehow doubt that you really have files with names like mm/dd/yy/fileIndex.zip. Forward slashes are not valid characters for file names in Windows.

Resources