0

I'm trying to create a bat file to remove -en-us from this mutiple files:

ie. monthly sales and cancellations-en-us.pdf would be renamed to monthly sales and cancellations.pdf.

This script below would work on local machine. However, if I define a filesharelocation which is a shared drive, and run it then I got this error:

File Not Found The system cannot find the file specified.

Below is the script i'm using:

@echo off
setlocal enabledelayedexpansion
set deletestring=-en-us
set filesharelocation=\\companyname\DEV\Testing
for /f "delims==" %%F in ('dir %filesharelocation% /b /l *-en-us.pdf ^| find "%deletestring%"') do (
    set oldfilename=%%F
    set newfilename=!oldfilename:%deletestring%=!
    Ren "!oldfilename!" "!newfilename!"
    )

Could anyone help? Thanks.

1 Answer 1

1

As it is coded, %%F returns the content in the output of dir command, that is, only the file name without path.

If it is executed from the same directory that contains the files, there is no problem, but when you run it agains the content of other drive/folder, as %%F does not contain the path to the file, and the files are not in the same folder than the batch file, the line

Ren "!oldfilename!" "!newfilename!"

(with oldfilename obtained from %%F) does not contain any path, only filenames.

Simplest solution is to add the path.

Ren "%filesharelocation%\!oldfilename!" "!newfilename!"

or, you can change the current drive to the folder with files

pushd "%filesharelocation%"
for /f "delims==" %%F in .....
    .....
)
popd
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, using this works: pushd "%filesharelocation%" for /f "delims==" %%F in ..... ..... ) popd

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.