Batch script is going in loop

Microsoft Windows server 2003 enterprise
December 10, 2009 at 06:02:49
Specs: Win Server 2003, intel
Hi All,

I am using below script for extracting all the .Zip files and placing in my target folder.

The scripting is working fine but after the script has unzipped and moved all the the files to the target directory..,then also the script is not commiun out ,it is showing message as
inflating the zip files.

Example:- suppose if there are 10 .Zip files the script is unzipping all the 10 files and placing in the target folder,but it shows me on the batch window that it is unzipping these files untill i abort that job,

Pls tell me how to make the script comeout of the loop after the job is done.

@echo off & setLocal EnableDELAYedExpansion
pushd C:\Data
unzip c:\Source\*.ZIP
popd
for /f "tokens=1,2,3 delims=\ " %%a in ('dir/s/b/a-d C:\Data') do (
if exist c:\target\%%c (
move C:\Target\%%c C:\Exists
echo %%c was moved at %date% %time% >> c:\unzip.log
copy C:\Data\%%c C:\Target
) else (
copy C:\Data\%%c C:\Target
)
)

Thanks


See More: Batch script is going in loop

Report •


#1
December 10, 2009 at 12:49:51
you might be having a problem with the script running at the same time that unzip is still working. since unzip is creating files into the \data directory (i think, due to pushd \data? or are they going into source?) If that is the case, the loop in your script is based on a directory that is changing content.
try using start:
start /wait unzip c:\Source\*.ZIP
popd...

other than that, the loop looks correct to me and you should not have to exit, (IF i'm understanding everything correctly).


Report •

#2
December 11, 2009 at 01:12:41
I don't think START /WAIT is very different from not starting and not waiting.

I think you have some issues with the FOR loop. Nevermind that you don't use the first 2 tokens. I think you're getting tripped up by trying to chunk up the path to the file with \ delim.

I would just get the file NAME with dir/b/a-d.


=====================================
Helping others achieve escape felicity

M2


Report •

#3
December 11, 2009 at 02:24:35
A couple of questions.

1. Which unzip utility are you using? Is it a recent version of the Win32 Console application (not DOS) from Info-ZIP, ported under GnuWin32 or Cygwin?

2. This bit of your code:

for /f "tokens=1,2,3 delims=\ "

treats backslashes and spaces as delimiters. Surely you don't want to treat spaces as delimiters, do you?

Also, after answering the above, could you place some ECHO statements inside and outside your loop for debugging purposes. E.g. inside the loop:

ECHO ***DBG1*** a="%%a" b="%%b" c="%%c"
and after the end of the loop:
ECHO ***DBG2*** Now outside loop, expecting to exit.


Report •

Related Solutions

#4
December 11, 2009 at 11:48:28
Run your script with "echo on" and see why he loops too much ... it should be obvious

Report •

#5
December 17, 2009 at 21:11:26
Hi ,

BElow are my Echo statements results

***DBG1*** a="C:" b="Data" c="FolderA"

***DBG2*** a="%a" b="%b" c="%c"

IF my folder is contains small no of files insidse the .Zip then the Batch is working fine and it the .Zip contains more than 300 files it is going in the loop. I tried all possible things but nothing seems to work.

How can I resolve this?

Thanks


Report •

#6
December 17, 2009 at 22:07:29
does your system have tasklist? if so, have your script run a test imm. after "unzip c:\source\*.zip":
unzip c:\source\*.zip
:: test data...
for /f %%z in ('tasklist /fi "imagename eq unzip.*" /fo "table" /nh') do echo %%z
pause

see if unzip shows up. if so, might indicate problem...
if unzip runs as tsr it might still be running bkgd.
(it's hard to create a test-arrangement with unzip and multiple files, so i'm not trying to go there...)


Report •

#7
December 18, 2009 at 01:13:43
Also, this debug trace:

***DBG1*** a="C:" b="Data" c="FolderA"

suggests you are copying whole folders, rather than just files. If
the folder contains lots of files, it could take a very long time to
copy, and you may think it is looping when it is just copying files
and will exit eventually.


Report •

#8
December 18, 2009 at 05:16:47
Post your current script.


=====================================
Helping others achieve escape felicity

M2


Report •

#9
December 18, 2009 at 05:19:16
Hi,

Below is the script which i am using.

@echo off & setLocal EnableDELAYedExpansion
pushd C:\Data
unzip c:\Source\*.ZIP
popd
for /f "tokens=1,2,3 delims=\ " %%a in ('dir/s/b/a-d C:\Data') do (
if exist c:\target\%%c (
move C:\Target\%%c C:\Exists
echo %%c was moved at %date% %time% >> c:\unzip.log
copy C:\Data\%%c C:\Target
) else (
copy C:\Data\%%c C:\Target
)
)


Thanks


Report •

#10
December 18, 2009 at 08:00:57
Like I said in #2 I would use the NAME.

I don't know why you popd out of c:\data and then use that berserk for loop.


=====================================
Helping others achieve escape felicity

M2


Report •

#11
December 19, 2009 at 01:14:52
M2, in what way is the for loop berserk? How does dir/s/b/a-d not
get the name? It does get the name, the problem is %%c is
sometimes the name of a folder (e.g. in
"c:\data\Folder1\subfolder2\filename.ext", %%c is "Folder1")

Report •

#12
December 20, 2009 at 01:06:30
yep, you got it


=====================================
Helping others achieve escape felicity

M2


Report •

#13
December 20, 2009 at 05:23:01
i thought the "/a-d" kept foldernames out? (although the folders themselves will be descended)

Report •

#14
December 20, 2009 at 23:31:33
Yes , /a-d does indeed keep folder names out, but it still gives
you the full pathnames of individual files. The for loop then picks
up a substring from this pathname to a file, which is in the
middle of the pathname, so it's a folder name.

Report •

#15
December 21, 2009 at 00:10:19
Hi klint,

yep, yep & yep

That's why I said the loop is berserk. It's hoping that %%c will be a file name.

I would skip the popd and just do dir/b/a-d and be guaranteed of getting file names.


=====================================
Helping others achieve escape felicity

M2


Report •


Ask Question