Hello, I am trying to execute a certain task where i am required to read files (one at a time) from a folder which can have undefined number of files. I need to be able to MOVE the first file in the folder to a new location and then execute another task with another batch file.The main aim is to read one files at a time instead of doing a . which will read all files at once. Any help would be appreciated ! Thanks
+1
A:
I think you might want to take a look at forfiles
:
Selects and executes a command on a file or set of files. This command is useful for batch processing.
Andrew Hare
2009-07-07 20:53:47
But I would like to have a single batch file for copying 1 files at a time, lets say I have 5 batch files as 5 Steps (each independent of other step) and the first step needs to read the first file in the folder instead of reading all files in a loop since this step will have no connection with Step2.
Murtaza RC
2009-07-07 20:59:32
In short is there a way to copy the first file in the folder instead of copying all files?
Murtaza RC
2009-07-07 21:03:06
+1
A:
You can use a for command something like this:
for /R c:\test\src %i IN (*.*) DO (
MOVE %i C:\test\dest
YourBatch.bat C:\test\dest\%~nxi
)
If you are putting this command in a batch file you will need to double up the % symbols like this:
for /R c:\test\src %%i IN (*.*) DO (
MOVE %%i C:\test\dest
YourBatch.bat C:\test\dest\%%~nxi
)
In the YourBatch.bat file access the file name using %1% something like this:
@echo off
type %1%
EDIT:
To only process one file simply exit at the end of the first loop:
for /R c:\test\src %%i IN (*.*) DO (
MOVE %%i C:\test\dest
YourBatch.bat C:\test\dest\%%~nxi
exit
)
Martin Brown
2009-07-07 21:26:18
It seems the code you have provided runs continuously in a loop for (*.*) I would like it to stop after reading the first file.
Murtaza RC
2009-07-07 21:37:52
The ~nx in the middle of %i means that you only get the file name and extention. n = file name, x = extention.
Martin Brown
2009-07-07 21:41:21
I've added a version that exits after the first itteration. I tried using goto :EOF but that doesn't seem to work.
Martin Brown
2009-07-07 21:52:42
A:
here is another way to do it. it uses some extensions to the SET command:
@echo off
setlocal ENABLEDELAYEDEXPANSION
FOR /f %%a IN ('dir /b') DO (
CALL SET /a x = !x! +1
if !x! == 1 (
REM do your work here. the call to move is an example
CALL ECHO moving %%a
CALL MOVE %%a ..
)
)
here are some details
akf
2009-07-07 22:18:33