I am trying to create a batch file (filehandling.bat)
I have a file in C:\Users\username\Downloads called "hita_2013_11_05_19_11_38.csv" where the date/time changes according to the date/time it is created
I need to rename this file to "hita.csv"
The problem comes because their will always be an existing "hita.csv" file in the directory that needs to remain there unless overwritten with the above file so the REN function isn't working because it is not overwriting the file
I also need it to make no changes(do nothing) to the existing "hita.csv" file if the "hita_2013_11_05_19_11_38.csv" file doesn't exist.
I've tried the following commands and can't get any to work: REN, MOV, ROBOCOPY /MOV
I've also tried:
IF EXIST "C:\Users\username\Downloads\Hita*.csv" (
DEL C:\Users\username\Downloads\Hita.csv
REN "C:\Users\username\DownloadsHita*.csv" Hita.csv
) ELSE (
Echo The file was not found.
)
but this command still deletes the "hita.csv" file for some reason
Change your test to IF EXIST "C:\Users\username\Downloads\Hita_*.csv" (note the underscore before the '*'). The * wildcard matches 0 or more characters, so it will match Hita.csv, Hita_01.csv, or HitaXYZ.csv.
Adding the underscore makes it only match files starting with Hita_ instead.
Your hita.csv matches the wildcard search of Hita*.csv, so it will delete it.
I believe you want your wildcard search to be Hita?*.csv. The question mark should act as an "exactly one", while the asterisk is "zero or more" characters, which means Hita.csv will not match that criteria.
Related
I want to copy the files from current folder to another location like (CURRENT FOLDER) file name like this (F0#CGDBANG000947532#) to another location like (\10.10.10.1\BasketsIn) with user name in the file name like (F0#CGDBANG000947532#logesh) at the end F0#CGDBANG000947532# copy to F0#CGDBANG000947532#username
thanks
Easy:
copy "c:\A" "d:\%username%_A"
EDIT
finally, after some of your comments here and in your other question, I understood your request (I think).
#echo off
for %%i in (%*) do if /i "%%~xi"==".eps" copy "%%i" "\\10.10.14.13\adman\in\displ\%%~ni%username%.%%~xi"
pause
If your file names do not have an extension or if you want the user name to be appended after the very end of the name (i.e. even after the extension), you could just use the following simple command:
COPY * \10.10.10.1\BasketsIn\*%USERNAME%
where USERNAME is a system environment variable that resolves to the user name of the current user.
If, however, you have names with extensions and you want the user name to be appended after the file name but before the file extension, you could use the ? mask character like this:
COPY * \10.10.10.1\BasketsIn\???????????????????????????????%USERNAME%.*
Just make sure you have provided enough ?s to cover the longest possible name in your case. If you are interested, you can learn more about this method in this excellent, in-depth Super User answer by dbenham:
How does the Windows RENAME command interpret wildcards?
One more note: this method may not work as expected with file names that have "multiple" extensions, i.e. like some.txt.doc.
How would I go about making a program that scans for a certain file inside a certain directory, not on the C:\ drive?
I know that IF EXIST does find a certain file, but I want it to to only try to find a certain file inside a certain directory, not on the C:\ as a whole.
Add the full path and filename to the IF EXIST and it will only check in that specified folder.
IF EXIST "C:\folder\folder2\file.ext" ...
Edited by Magoo - added quotes which are required if the full path contains spaces or some other characters
I have code that looks like this
%original%-added.txt
So if the original filename is
blue.txt
My code modifies that blue.txt file and the new file is then named
blue.txt-added.txt
How can I remove that first ".txt" so that I only get
blue-added.txt
Most likely a simple REN command is all you need. I'm assuming you always want to preserve the original file extension.
ren "%original%" "?????????????????????-added.*"
Just make sure there are at least as many ? as there are characters in the original name up until the .
Here are some results you can expect
original new
-------- ------------
blue.txt blue-added.txt
part1.part2.txt part1-added.part2.txt
You could use wildcards in your source file mask. The following would append "-added" to the base name of all .txt files:
ren *.txt ?????????????????????-added.*
See How does the Windows RENAME command interpret wildcards? if you want to understand why this works.
Try like this :
%original:~0,-4%-added.txt
I have a script that changes particular string within files names (the file stores in "my_folder"):
Set objFso = CreateObject("Scripting.FileSystemObject")
Set Folder = objFSO.GetFolder("g:\my folder")
For Each File In Folder.Files
sNewFile = File.Name
sNewFile = Replace(sNewFile,"._epf","_v0_1._epf")
if (sNewFile<>File.Name) then
File.Move(File.ParentFolder+"\"+sNewFile)
end if
Next
the scrpit works fine if there are no folders under "g:\my folder", otherewise, if there are folders in "my folder" and the name of one (or more) of those folders are similiar to some file name, the scrip cause unwanted results like multiplying the replace string.
for example if "my folder" contain:
hello (folder)
hello_.epf (file)
then the script will eventually change the file name to:
hello_v0_1_v0_1._epf (unwanted result)
and i want the result to be:
hello_v0_1._epf
I'll appreciate quick help in this manner.
thanks.
I haven't bothered to try to figure out where your VBScript is going wrong. But you tagged your question with batch-file, batch, and batch-rename.
Here is a simple one-liner that can be run from the command prompt that will do what you want. It doesn't even need a batch script.
for %F in ("g:\my folder\*._epf") do #ren "%F" "%~nF_v0_1%~xF"
If you want to run the command within a batch script, then you need to double all percents.
#echo off
for %%F in ("g:\my folder\*._epf") do ren "%%F" "%%~nF_v0_1%%~xF"
EDIT
The above will append a new version suffix to each file name, before the extension.
If you want to replace an existing version number, then the solution is even easier. I'm assuming that your version always starts with _v, and v will never occur in the file extension.
ren "g:\my folder\*_v0_1._epf" "*v0_2.*"
The above command renames all files that end with _v0_1._epf. It preserves all characters up through the last occurance of v in the name, then adds the new version number, and finally appends the original extension.
See How does the Windows RENAME command interpret wildcards? for rules on how REN uses wildcards.
MY QUESTION:
I have the same situation as Rishi. I have a number of versions of the same song by the same artist that appear on different CD's. If I use the batch command as written, will DOS overwrite songs with the same name, even if the file size is different for each unique file?
PREVIOUS QUESTION: DOS command to move all files in subdirectories one level up
REFERENCE Rishi asked the question on Jan 15th:
"I have a folder with a lot of sub folders with one or more files in each. I am trying to write a batch file that moves all those files to C:\songs (for example).
C:>FOR /R C:\Test %i IN (*) DO MOVE %i C:\Songs
The folders Test and songs exist, but I get an error saying
%i was unexpected at this time.
What am I doing wrong?"
ANSWER WAS
"FOR /R %i IN (C:\Test*) DO MOVE "%i" C:\Songs
In a batch file, it has to be %%i. Weird quirk of batch."
Within a given folder there can only be one version of a file with a given name. When executed within a batch, the MOVE command will automatically overwrite any pre-existing file of the same name. So the answer to your question is - YES, a file with the same name will be over-written, even if it has a different file size. (Note - if you are using Windows XP then you are not using DOS)
You can prevent a batch move from overwriting an existing file by piping N to MOVE with the -y option:
echo n | move /-y "%%~i" "C:\songs\"
If you want to copy and preserve both versions into the same folder, then at least one version will have to be renamed. You will have to decide what kind of naming scheme you want to use before you can begin coming up with a solution.