naming a file in a certain way

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ilpo
Posts: 3
Joined: 28 Dec 2015 06:48

naming a file in a certain way

#1 Post by ilpo » 28 Dec 2015 06:57

Hi, all!

I need to fetch (using ftp) a file from server to a directory on our server

In our directory are all the previous files
They are named like …XX010.dat, XX011.dat

File comes with original name ABC.dat

How do I change the name like this:
the next file must be renamed like XX012.dat

Thanks,
Ilpo

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: naming a file in a certain way

#2 Post by foxidrive » 28 Dec 2015 07:05

You count the numbers and rename the file that is downloaded.

The set /a command can add one to the highest number
but the entire task is dependent on the actual names - because parsing the names is part of the job.

ilpo
Posts: 3
Joined: 28 Dec 2015 06:48

Re: naming a file in a certain way

#3 Post by ilpo » 29 Dec 2015 01:51

Hi, folks!

I was wondering how to get rid of this manual renaming (SET /P and so on).
It would be best if it all could be done automatically within this batch:

(local is the place where renamed files are left, but the next phase needs
that new name.)

@ECHO OFF
setlocal
dir XYZ0*.dat
SET /P file=Give next number like 0257:

REM if exist XX%file%.dat goto run
REM get file with ftp if there is one

ECHO open 190.xx.xxx.x>ftp1.ini
ECHO user>>ftp1.ini
ECHO passwd>>ftp1.ini
ECHO cd inbox>>ftp1.ini
ECHO get ABC%file% XYZ%file%.dat>>ftp1.ini
ECHO bye>>ftp1.ini

ftp -s:ftp1.ini

if not exist ABC%file%.dat goto miss
rem if exist ABC%file%.dat goto run
goto run

:miss
ECHO There is no ABC%file%.dat.
goto end

:run

ECHO open serverX>ftp2.ini
ECHO user>>ftp2.ini
ECHO passwd>>ftp2.ini
ECHO cd m3server>>ftp2.ini
ECHO put XYZ%file%.dat XYZ%file%.dat>>ftp2.ini
ECHO bye>>ftp2.ini

ftp -s:ftp2.ini

:end


Thanks!
Ilpo

ilpo
Posts: 3
Joined: 28 Dec 2015 06:48

Re: naming a file in a certain way

#4 Post by ilpo » 29 Dec 2015 02:52

Hi folks!

I need to specify my case.
local is the place where files are left and where the batch is.

I was thinking if it's possible to automate that renaming process - for now it has been made manually?
It's the second phase that actually needs that new name.
So how to rename (SET /P phase) file within the automated batch?
Batch:

@ECHO OFF
setlocal
dir XYZ0*.dat
SET /P file=Give next number like 0257:

REM if exist ABC%file%.dat goto run
REM get file with ftp if there is one

ECHO open 190.xx.xxx.x>ftp1.ini
ECHO user>>ftp1.ini
ECHO passwd>>ftp1.ini
ECHO cd inbox>>ftp1.ini
ECHO get ABC%file% XYZ%file%.dat>>ftp1.ini
ECHO bye>>ftp1.ini

ftp -s:ftp1.ini

if not exist ABC%file%.dat goto miss
rem if exist ABC%file%.dat goto run
goto run

:miss
ECHO There is no ABC%file%.dat.
goto end

:run

ECHO open serverX>ftp2.ini
ECHO user>>ftp2.ini
ECHO passwd>>ftp2.ini
ECHO cd m3server>>ftp2.ini
ECHO put XYZ%file%.dat XYZ%file%.dat>>ftp2.ini
ECHO bye>>ftp2.ini

ftp -s:ftp2.ini

:end

Thanks,
Ilpo

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: naming a file in a certain way

#5 Post by foxidrive » 29 Dec 2015 03:05

I gather you just want your code below to increment the number in the file variable.

Code: Select all

@ECHO OFF
setlocal
dir XYZ0*.dat
SET /P file=Give next number like 0257:


You parse the brief directory listing in a for loop and extract the number, and add one to it.

As mentioned in my last post, the filenames are important because the way the listing is parsed depends on the actual filename format.

See here: viewtopic.php?f=3&t=6108

Post Reply