Batch file to detect only part of a filename - windows

I need a snippet of a batch file that can detect part of a filename, then rename it.
Note that the numbers after the filename change randomly but "file" is always the same.
Example:
filename: file143424
There will always be only one file that needs to be renamed in the folder. The script will delete older versions of the file. For instance, I put a first file inside the folder and the script in there too. I then run the script, which renames it to just file. Then if I put a new file in, the script, when run again, will recognize it from the "file" prefix and rename it to file after deleting the old one.

Excuse me. Your question is incomplete, I think. You want to rename "file134342" to what?
In your comment you said: rename it to just "file", but this works only if there is just one file with that name. Anyway, here it is:
for %%f in (file*) do ren %%f file
If this is not what you want, then give us more details (you showld always give full details from the very beginning).

You can check for the first four characters that way:
if "%filename:~0,4%"=="file" ...

#Aacini's suggestion should work fine in your case. But you could do very well without a for loop, a single REN (RENAME) command would be enough:
RENAME file* file

Related

Windows batch autoanswer XCOPY /i

I have created a batch file which uses FOR command to read a file of FROM Directory, FROM File, TO Directory, TO File as the parameters. (I am giving the files NEW names in the destination)
Everything works great until I add a new file to the mix.
In XCOPY /i option says it is a directory (which is NOT true). IF I don't use /i it wants to know if it is a file or a directory. It is ALWAYS a File. Is there a way I can autoreply or does someone have another suggestions.
echo f|xcopy [options] [files*]
A short investigation did not yield an easy command line parameter solution.
If I got you right, you are trying to copy a file from one directory to a new directory + give it a new name in the new directory. Copying to the new directory should work fine when you terminate target directory name with a tailing '\' (backslash) - this should result in a file with the same name in the target dir. Renaming this file to the new name will be straight forward. However, it's two commands instead of one...
In case this does not work for you: maybe you can illustrate your qn with a sample / snippet of the batch file?

How to use the list files in a given directory via batch file without showing the full path?

I have searched for this everywhere so I hope it has not already been asked, but I have a batch file where the user can write his / her own 'scripts' if you will. When the batch file is ran for the first time it will make a directory under %appdata%\Mellow\Mango\scripts and these scripts will simply be .txt files. Anyway...
I am trying to list the 'scripts' to the user by using dir /b /s *.txt and the output is C:\Users\Tate\AppData\Roaming\Mellow\Mango\scripts\template.txt
My question is, sorry if I got off-topic before, how to display only template.txt and not the full file path. I simply would like to list all .txt files contained in the scripts folder. Thanks in advanced!
Current Output:
C:\Users\Tate\AppData\Roaming\Mellow\Mango\scripts\template.txt
C:\Users\Tate\AppData\Roaming\Mellow\Mango\scripts\another_script.txt
Desired Output:
template.txt
another_script.txt
Simply remove the /s
dir /b *.txt
That should do it :)

how to batch replace the files in a directory windows vista(from .txt.txt to .txt)

I have a couple of flat files(.txt) in a directory.all those files are in the format *.txt.txt so i want to rename it to *.txt ?Is there any simple way to rename all together?
when I tried ren *.txt.txt *.txt is is not working
Any experts please suggest?It is amazing I have not got any answer yet
Please be noted that I need an out of the format filename.txt.
This should work
ren *.txt.txt *.
The reason your command didn't work is because, to windows, the file file.txt.txt is called file.txt with a .txt extension.
Only the last extension is the real extension, the first then becomes part of the filename, hence why your command changes it to what it already is.
If you did ren *.txt.txt *.pdf you would get file.txt.pdf.
My command will just remove the last one, thereby leaving the first, which then becomes the only and real extension.

changing directory stops %~dp0 from working

I have two batch file on the C:\ drive and am using %~dp0 command to use the path of the first script to make a copy the second batch script:
COPY %~dp0"Hello World.BAT" C:\"Hello World.bak"
Early in the script I am required to change to a sub directory off the root of the C:\ but this stops the above copy command from working the error I get is "the file cannot be found". If I stay in the root of the C:\ the copy command works perfectly. Any ideas why this is happening.
Another way to solve this would be to save %~dp0 in another variable at the beginning of your script.
#echo off
setlocal
set filepath=%~dp0
.
.
some code
.
.
cd away from original path
.
.
COPY "%filepath%Hello World.BAT" "C:\Hello World.bak"
That should work.
I am tempted to think the reason it is not working has to do with your quotes.
You have this:
COPY %~dp0"Hello World.BAT" C:\"Hello World.bak"
replace it with this:
COPY "%~dp0Hello World.BAT" "C:\Hello World.bak"
you need to wrap the entire path in quotes to be sure it will work. If you have:
C:\Program Files\Somefolder\
as your path and use the quotes how you have them it will turn out like this:
"C:\Program Files\Somefolder\""Hello World.bak"
and it won't work.
I haven't exactly worked out in my mind how changing the current directory causes the command to fail when it works before the change. But I notice that the quotes are not placed optimally. Spaces in the path would cause the command to fail, though it seems to me it should fail regardless of your current directory.
I would use:
COPY "%~dp0Hello World.BAT" "C:\Hello World.bak"
Moving the quote to the front of the 1st argument is potentially important. Moving it for the 2nd argument is not important since there are obviously no spaces in the path, but it looks better to me.
edit
After reading your question more carefully, I'm thinking there must be more to the story. If both batch files are in the root of the C drive, then your original posted code should work.
Try editing your script to diagnose what is happening. Put ECHO before the copy command so you can see what the script is attempting to do. (or simply make sure echo is on, but then it may be harder to find the correct line in the output.)
echo COPY %~dp0"Hello World.BAT" C:\"Hello World.bak"
If you still can't figure out what is wrong, post the results so others might help.

DOS Commands- Excluding files in a folder in xcopy

I have a folder containing many other sub-folders.
I am trying to write a batch file which will copy some of the folders to another place on my hard disk. I am using "xcopy" for this. I am facing following problem:
The folder structure is as shown below-
--FolderB1
---FolderB2
---FolderB22
---File1.txt
---File2.txt
---File3.txt
I have some .txt files inside "FolderB1", along with "FolderB2" and
"FolderB22" I want to copy "FolderB2" and "FolderB22" and skip ".txt"
files contained in "Folder B1"
I tried using /EXCLUDE: param of xcopy command, but it is not able to perform this operation. It does not work if I specify the exclusion as \FolderB1\*.txt or something of this sort.
The number of main folders is not known. It can be anything. Also, there is no fix pattern for names of ".txt" files. Have checked this question too, but did not help.
Alternate method or other pointers for the same would be a great help. Thanks in advance.
What you could try to do is to hide the files you don't want to copy, then execute the xcopy, and then unhide the files again.
Look at my answer of question Windows batch script to delete everything in a folder except one. That question was related do deleting files (excluding some files), but you can probably use the same trick for xcopy-ing files.

Resources