Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top