User User name Password  
   
Tuesday 19.8.2025 / 09:24
Search AfterDawn Forums:        In English   Suomeksi   På svenska
afterdawn.com > forums > software, operating systems and more > windows - software discussion > need help writing a batch file for the following task:
Show topics
 
Forums
Forums
NEED HELP writing a batch file for the following task:
  Jump to:
 
Posted Message
aweathe
Junior Member
_
10. May 2007 @ 12:38 _ Link to this message    Send private message to this user   
the shorter program you wrote takes some time to complete in the command window (5-10 secs)but nothing seems to be changed in the folders. I don't understand that one though! ;o)
Advertisement
_
__
Indochine
Senior Member
_
10. May 2007 @ 12:50 _ Link to this message    Send private message to this user   
have you tried the latest revised shorter one? (I changed it)

does it give any messages?

What you see if you go in the top folder and type dir /b /s


On m'a dit que je suis nul à l'oral, que je n'peux pas mieux faire !

This message has been edited since posting. Last time this message was edited on 10. May 2007 @ 12:51

Indochine
Senior Member
_
10. May 2007 @ 13:23 _ Link to this message    Send private message to this user   
There is something bugging me... the batch files I wrote are working perfectly on my machine, where I have a test setup of folders with rar files exactly as we discussed. In each subfolder is a set of subfolders, some with rars, some without.




There's something I'm missing, something that is different between our two systems, and I need to find out what it is.

Are you running Windows XP?

What happens when you go into the top folder, and type the following...

dir /s /b *.rar

Does it find any?

More relevant, does this find any?


@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set topfolder=%CD%
set unrarapp="C:\Program files\winrar\rar.exe"
for /F "delims==" %%a in ('dir /b /s *.rar') do (
	CD "%%~pa" & %unrarapp% e "%%~Dpnxa"
	)
cd %topfolder%



On m'a dit que je suis nul à l'oral, que je n'peux pas mieux faire !

This message has been edited since posting. Last time this message was edited on 11. May 2007 @ 00:44

Indochine
Senior Member
_
11. May 2007 @ 00:38 _ Link to this message    Send private message to this user   
updated version, see above


On m'a dit que je suis nul à l'oral, que je n'peux pas mieux faire !
aweathe
Junior Member
_
14. May 2007 @ 07:47 _ Link to this message    Send private message to this user   
I have found one thing that is causing a problem. The files are not (!) WinRAR files. (yes i know you're laughing at how I could have not realized this earlier).

They are labeled as WinRAR files by my computer because I have WinRAR. But their extensions are not ".rar"

The extensions are instead ".Z"

An example of one of the many filenames is "18685643.msg.Z"

Now, I have changed you're code (I'm working with the longer version becasue I can understand it) so that the statments including "*.rar" get changed to included "*.Z" However, this doesn't solve the problem.

I am able to decompress these files manually (one at a time) by right clicking them and chosing decompress.

But for some reason I cannot get them to decompress from the comand prompt. I have tried to do this with sigle files using simple WinRAR commands in the command propmt.

I get the same error message for both your code (with the "*.Z") and for the single file tests that I've been doing in the command prompt.

The error message is (given in the cmd prompt):
******************************************************************
RAR 3.62 Copyright (c) 1993-2006 Alexander Roshal 3 Dec 2006
Shareware version Type RAR -? for help

18685818.msg.Z is not RAR archive
No files to extract

(4) Finished

Press any key to continue . . .
************************************************************

I've been trying to figure this out...

It also doesn't help if I simply rename the file to ".RAR" (ie 18685818.rar). The same error message is given.
aweathe
Junior Member
_
14. May 2007 @ 07:53 _ Link to this message    Send private message to this user   
Originally posted by Indochine:
There is something bugging me... the batch files I wrote are working perfectly on my machine, where I have a test setup of folders with rar files exactly as we discussed. In each subfolder is a set of subfolders, some with rars, some without.




There's something I'm missing, something that is different between our two systems, and I need to find out what it is.

Are you running Windows XP?

What happens when you go into the top folder, and type the following...

dir /s /b *.rar

Does it find any?

More relevant, does this find any?


@echo off

SETLOCAL ENABLEDELAYEDEXPANSION
set topfolder=%CD%
set unrarapp="C:\Program files\winrar\rar.exe"
for /F "delims==" %%a in ('dir /b /s *.rar') do (
CD "%%~pa" & %unrarapp% e "%%~Dpnxa"
)
cd %topfolder%


My responses:
1. The program "SETLOCAL ENABLEDELAYEDEXPANSION" returns "File not found"

2. Yes I'm running windows XP

3. Your file structure diagram is the same as how it is on my computer.
Indochine
Senior Member
_
14. May 2007 @ 08:20 _ Link to this message    Send private message to this user   
OK. I was getting worried that my code had run amok and overwritten your hard drive!

It wouls appear that archive files with the extension .z are files compressed with the Unix "Compress" program. You can get an equivalent DOS/Windows program called gzip.exe here...

ftp://sis.agr.gc.ca/PUB/cansis/gzip.exe

Place it in a folder & be aware of its path & location

eg C:\Program files\Gzip\gzip.exe

the syntax to decompress is gzip -d (filename)

After decompression the .z extension is gone, and the archive is REPLACED by its decompressed counterpart.

Thus...

gzip -d "18685643.msg.Z" would result in the file ""18685643.msg"

I will cook up a batch file shortly...










On m'a dit que je suis nul à l'oral, que je n'peux pas mieux faire !
aweathe
Junior Member
_
14. May 2007 @ 09:57 _ Link to this message    Send private message to this user   
OK I've saved that file to the dir you suggested.

Could you tell me what "delims==" means? The loop doesn't seem to be working in the long version... and this is one part I don't understand.

I'm changing the long version to run with the gzip command instead -- attempting to ;o)
Indochine
Senior Member
_
14. May 2007 @ 10:02 _ Link to this message    Send private message to this user   
for /F "delims==" %%a in ('type "%topfolder%\subdirlist.txt"') do (

"delims==" tells FOR to treat each line produced by 'type "%topfolder%\subdirlist.txt"' as a complete line, ie not to split it into separate words.


On m'a dit que je suis nul à l'oral, que je n'peux pas mieux faire !
aweathe
Junior Member
_
14. May 2007 @ 10:03 _ Link to this message    Send private message to this user   
ok thanks!
Indochine
Senior Member
_
14. May 2007 @ 10:42 _ Link to this message    Send private message to this user   
The long versions of the file depended on the ability of the RAR.exe program to take a wildcard (*.rar) instead of a specific filename.

This technique is not possible with gzip.

here is a batch program I have just made that seems to work OK with .z files. I suggest you make a copy of your data and run it on that rather than the important stuff. I have added some comments to explain aspects of the code.


@echo off
REM I confess I am not sure if this 
REM is needed but it won't hurt
SETLOCAL ENABLEDELAYEDEXPANSION
set topfolder=%CD%
REM for each .z file in any subfolder
for /F "delims==" %%a in ('dir /b /s *.z') do (
	REM in respect of each file
	REM represented by %%a,
	REM %%~pa is its path (folder)

        REM change to its folder
	CD "%%~pa"

	REM Extract filename parameters
	REM %%~Da is its drive letter and colon
	REM %%~pa is its path (folder)
	REM %%~na is its filename
	REM %%~xa is its .extension
	REM can be combined thus...

	REM inform user of file being processed
	echo processing %%~Dpnxa

	REM call gzip with -d option (decompress)
	"C:\Program Files\gzip\gzip.exe" -d "%%~Dpnxa"
	)
cd %topfolder%



On m'a dit que je suis nul à l'oral, que je n'peux pas mieux faire !

This message has been edited since posting. Last time this message was edited on 14. May 2007 @ 10:46

aweathe
Junior Member
_
14. May 2007 @ 12:18 _ Link to this message    Send private message to this user   
Ok I'm going to try the new code you just put up.
I didn't try it until now because I was trying to make the longer version work... and I think I have. I think I understand the problem you were stating about the longer version when switching to ".z" files, but I didn't run into that problem.

Here is a copy of your original long version with my modifications. I think it works.. but would like your comments!

@echo off
REM place this batch file in the top folder
REM and run it from there


REM remember where we are
set topfolder=%CD%


if exist "%topfolder%\subdirlist.txt" del "%topfolder%\subdirlist.txt" > nul
if exist "%topfolder%\gzipdirlist.txt" del "%topfolder%\gzipdirlist.txt" > nul
echo.
echo (1) creating list of all sub folders...
echo.

pause

REM get a list of all the subfolders
REM under this one and put it in a temp file
dir /b /s /ad > "%topfolder%\subdirlist.txt"


echo all sub folders...
echo.
type "%topfolder%\subdirlist.txt"
echo.

echo.
echo (2) finding folders with gzips in them
echo.


REM in a loop check each one for gzip files
REM write folder name to another list if any found
for /F "delims==" %%a in ('type "%topfolder%\subdirlist.txt"') do (
cd %%a

if exist *.Z echo %%a >> "%topfolder%\gzipdirlist.txt"
)

pause

echo all sub folders with gzips in...
echo.
type "%topfolder%\gzipdirlist.txt"
echo.
pause
echo.
echo (3) extracting from gzip archives
echo.
echo ready to start...
echo.
pause
echo.
REM for each folder that contains gzips
REM enter folder and extract
for /F "delims==" %%a in ('type "%topfolder%\gzipdirlist.txt"') do (
echo.
echo expanding gzips in %%a
echo.

cd %%a

copy *.Z *.Y


"c:\program files\gzip\gzip.exe" -d *.Z


ren *.Y *.Z


)

REM clean up
rem del "%topfolder%\subdirlist.txt" > nul
rem del "%topfolder%\gzipdirlist.txt" > nul

REM go back to where we were
cd %topfolder%
echo.
echo (4) Finished
echo.
pause
echo.
aweathe
Junior Member
_
14. May 2007 @ 12:24 _ Link to this message    Send private message to this user   
It has been alot to take in for me and so I wanted to try to understand one thing at a time... so that's why I was working with the original... Man do I have a headache! ahh the joy of learning new skills!
aweathe
Junior Member
_
14. May 2007 @ 12:50 _ Link to this message    Send private message to this user   
I'm leaving for home now... but I just tried your latest version. It didn't work all the way (I'm too tired to figure out why...). The error message said "No such file or directory", for each iteration. (ie about 30 times or so, with the path directory in between)

Talk to you tomorrow.
Indochine
Senior Member
_
14. May 2007 @ 13:42 _ Link to this message    Send private message to this user   
Quote:

copy *.Z *.Y

good thinking.

Quote:

"c:\program files\gzip\gzip.exe" -d *.Z

Won't work, as i said before. Wildcard ("*.Z") won't work.

need to make further loop

see code in next post


On m'a dit que je suis nul à l'oral, que je n'peux pas mieux faire !

This message has been edited since posting. Last time this message was edited on 15. May 2007 @ 01:11

Indochine
Senior Member
_
15. May 2007 @ 01:10 _ Link to this message    Send private message to this user   
Further to my recent posts...

(1)

You need a later version of Gzip specifically for Win32

You can get the Gzip 1.3.11 installer here

http://dfn.dl.sourceforge.net/sourceforg...gzip-1.3.11.exe

You need to run it and select an installation directory.

By default it will create a subfolder structure under that directory
and the gzip.exe will be in a subdirectory called bin.

You can leave it there & use the full path in your batch files or move or copy it
where you want...

I put mine into c:\Program Files\Gzip to make things easier to remember.

(2)

The copy *.Z *.Y thing does not work. Wildcards are OK for REN but not for COPY.
Solution in this batch file, which seems to do the required things...

You can delete or comment out (by preceding with REM) the pause statements if
you want to run it unattended. They are there to provide a chance to hit CTRL + C and abort the batch during debugging really.

(3) This gzip supports wildcards which makes things simpler

(4) new code...

@echo off
REM place this batch file in the top folder
REM and run it from there
REM remember where we are
set topfolder=%CD%
if exist "%topfolder%\subdirlist.txt" del "%topfolder%\subdirlist.txt" > nul
if exist "%topfolder%\gzipdirlist.txt" del "%topfolder%\gzipdirlist.txt" > nul
echo.
echo (1) creating list of all sub folders...
echo.
pause
REM get a list of all the subfolders
REM under this one and put it in a temp file
dir /b /s /ad > "%topfolder%\subdirlist.txt"
echo all sub folders...
echo.
type "%topfolder%\subdirlist.txt"
echo.
echo (2) finding folders with gzips in them
echo.
REM in a loop check each one for gzip files
REM write folder name to another list if any found
for /F "delims==" %%a in ('type "%topfolder%\subdirlist.txt"') do (
cd %%a
if exist *.Z echo %%a >> "%topfolder%\gzipdirlist.txt"
)
pause
echo all sub folders with gzips in...
echo.
type "%topfolder%\gzipdirlist.txt"
echo.
pause
echo.
echo (3) extracting from gzip archives
echo.
echo ready to start...
echo.
pause
echo.
REM for each folder that contains gzips
REM enter folder and extract
for /F "delims==" %%a in ('type "%topfolder%\gzipdirlist.txt"') do (
cd %%a
for /F "delims==" %%b in ('dir /b *.z') do (
echo copying "%%b" to "%%~nb.y"
copy "%%b" "%%~nb.y"
)
echo.
echo expanding gzips in %%a
echo.
"c:\program files\gzip\gzip.exe" -d -v *.z
ren *.Y *.Z
)
REM clean up
del "%topfolder%\subdirlist.txt" > nul
del "%topfolder%\gzipdirlist.txt" > nul
REM go back to where we were
cd %topfolder%
echo.
echo (4) Finished
echo.
pause
echo.

On m'a dit que je suis nul à l'oral, que je n'peux pas mieux faire !

This message has been edited since posting. Last time this message was edited on 15. May 2007 @ 01:20

Indochine
Senior Member
_
15. May 2007 @ 05:42 _ Link to this message    Send private message to this user   
short version (no pauses, less messages, just does it)

@echo off
echo Started...
set topfolder=%CD%
if exist "%topfolder%\subdirlist.txt" del "%topfolder%\subdirlist.txt" > nul
if exist "%topfolder%\gzipdirlist.txt" del "%topfolder%\gzipdirlist.txt" > nul
dir /b /s /ad > "%topfolder%\subdirlist.txt"
for /F "delims==" %%a in ('type "%topfolder%\subdirlist.txt"') do (
cd %%a & if exist *.Z echo %%a >> "%topfolder%\gzipdirlist.txt"
)
For /F "delims==" %%a in ('type "%topfolder%\gzipdirlist.txt"') do (
cd %%a & copy *.Z *.Y
"c:\program files\gzip\gzip.exe" -d -q *.z & ren *.Y *.Z
)
del "%topfolder%\subdirlist.txt" > nul & del "%topfolder%\gzipdirlist.txt" > nul
cd %topfolder%
echo Finished

On m'a dit que je suis nul à l'oral, que je n'peux pas mieux faire !

This message has been edited since posting. Last time this message was edited on 15. May 2007 @ 07:17

aweathe
Junior Member
_
15. May 2007 @ 07:11 _ Link to this message    Send private message to this user   
OK here's what's going on:

1) I've ran both of the two latest scripts you've posted (the long version and the short version) and they both work.

2) The last script that I posted (at 14. May 2007 @ 16:18) (modified from you're original long version) also seems to work. Ie: after copying the folder (and subfolders) so that I have 3 new folders to test these codes on, when I run each of these codes they all seem to work. And thus produce the same result. Can you tell me where I can look for difference/problems? Or what is the risk to running the code that I modified? I didn't understand that yet. The *.z seems to have worked for the copy command...?

3) I have run each of these programs using the same gzip.exe file: the original link you sent me. It seems to be working... should I still download the newer version?


PS when the program worked for the first time... man then it was all worth it! : huge time saver for sure.
aweathe
Junior Member
_
15. May 2007 @ 07:31 _ Link to this message    Send private message to this user   
The only line I don't understand about you're latest script is:

copy "%%b" "%%~nb.y"

Can you tell me what "%%~nb.y" mean in this line?
aweathe
Junior Member
_
15. May 2007 @ 07:35 _ Link to this message    Send private message to this user   
The only line I don't understand about you're latest script is:

copy "%%b" "%%~nb.y"

Can you tell me what "%%~nb.y" means in this line?
Indochine
Senior Member
_
15. May 2007 @ 07:48 _ Link to this message    Send private message to this user   
Originally posted by aweathe:
2) The last script that I posted (at 14. May 2007 @ 16:18) (modified from you're original long version) also seems to work.
Quote:
The last script that I posted (at 14. May 2007 @ 16:18) (modified from you're original long version) also seems to work. Ie: after copying the folder (and subfolders) so that I have 3 new folders to test these codes on, when I run each of these codes they all seem to work. And thus produce the same result. Can you tell me where I can look for difference/problems? Or what is the risk to running the code that I modified? I didn't understand that yet.
I should stick with your modified version of my long code. It has the advantage of being familiar to you; also the alterations which made it work are yours, and you are entitled to them!

I would like to say "very well done" in this regard!

I wrote the long version like that, heavily commented, with frequent reassuring messages and PAUSE commands because I wanted to be sure at each step that it was working OK, was in the right folder, was doing the right things, etc. The shorter version is essentialy the same code but with all the hand holding and comments removed, for the sake of compactness.

For example, in Windows NT prompt (Win2K, XP, Vista) you can combine short lines into one line if they are always executed one after the other -

@echo off
CLS
echo hello
DIR

could be replaced by

@echo off & DIR & echo hello & DIR

I tend to write out my batch files in a long form and spend time getting them right, and if I am going to use one often, I might well "tidy it up" in this way.

Quote:
The *.z seems to have worked for the copy command...?
It does work; I was mistaken thinking it didn't. I have revised my short version above to take account of this.

Quote:
3) I have run each of these programs using the same gzip.exe file: the original link you sent me. It seems to be working... should I still download the newer version?
I found the earlier version worked but showed strange characters on the screen when run in command mode. It dates from 1993, and the later version 1.3.11 which was compiled for Win32 (Win95 onwards) doesn't do this.

Obviously it is a matter of preference.

Quote:
PS when the program worked for the first time... man then it was all worth it! : huge time saver for sure.
It's a great feeling.





On m'a dit que je suis nul à l'oral, que je n'peux pas mieux faire !
Indochine
Senior Member
_
15. May 2007 @ 08:12 _ Link to this message    Send private message to this user   
Quote:
The only line I don't understand about you're latest script is:

copy "%%b" "%%~nb.y"

Can you tell me what "%%~nb.y" means in this line?
I cooked that up because I mistakenly thought that you couldn't do a

copy *.z *.y

operation.

with FOR in NT family Windows, you can extract information from variables.

If %%b is a file, (in this case a .z file)

%%~nb is the filename without any extension (ie with ".z" missing)

so if %%b expands to apple-pie.z
then %%~nb expands to apple-pie
and %%~nb.y expands to apple-pie.y

Type FOR /? at the prompt to get full info...

here I represents any variable letter (I used letter b)

(In batch files you prefix an extra % sign)

%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only (and colon character) eg D:
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file

The modifiers can be combined to get compound results:

%~dpI - expands %I to a drive letter and path only
%~nxI - expands %I to a file name and extension only
%~fsI - expands %I to a full path name with short names only

On m'a dit que je suis nul à l'oral, que je n'peux pas mieux faire !

This message has been edited since posting. Last time this message was edited on 15. May 2007 @ 08:13

aweathe
Junior Member
_
15. May 2007 @ 09:05 _ Link to this message    Send private message to this user   
great thanks. that makes it clear.
aweathe
Junior Member
_
15. May 2007 @ 09:30 _ Link to this message    Send private message to this user   
I'm starting on my second task. But thanks to your help I think I can do it almost all on my own.

I have run into a small problem:

If I want to check match up files from the following two lists:

I17CI
I33MG
I35NA
I47ZA

IS17.site
IS33.site
IS35.site
IS47.site

What would the true/false testing statement look like when comparing files from one list with files from the other list, if I wanted it to return true only when the numbers (within the file names) match?


And if you're getting a little tired of my questions, please just let me know... I would understand completely... you've helped a ton already.

This message has been edited since posting. Last time this message was edited on 15. May 2007 @ 09:32

Advertisement
_
__
 
_
Indochine
Senior Member
_
15. May 2007 @ 09:44 _ Link to this message    Send private message to this user   
Originally posted by aweathe:
If I want to check match up files from the following two lists:

I17CI
I33MG
I35NA
I47ZA

IS17.site
IS33.site
IS35.site
IS47.site

What would the true/false testing statement look like when comparing files from one list with files from the other list, if I wanted it to return true only when the numbers (within the file names) match?

Are the lines in the files always in this format...

first file

one letter, two figures, two letters

second file

two letters, two figures and ".site"?

Are all the four examples you gave "hits"?

so
A17QW
B17TR

would match

SA17.site ?





On m'a dit que je suis nul à l'oral, que je n'peux pas mieux faire !
 
afterdawn.com > forums > software, operating systems and more > windows - software discussion > need help writing a batch file for the following task:
 

Digital video: AfterDawn.com | AfterDawn Forums
Music: MP3Lizard.com
Gaming: Blasteroids.com | Blasteroids Forums | Compare game prices
Software: Software downloads
Blogs: User profile pages
RSS feeds: AfterDawn.com News | Software updates | AfterDawn Forums
International: AfterDawn in Finnish | AfterDawn in Swedish | AfterDawn in Norwegian | download.fi
Navigate: Search | Site map
About us: About AfterDawn Ltd | Advertise on our sites | Rules, Restrictions, Legal disclaimer & Privacy policy
Contact us: Send feedback | Contact our media sales team
 
  © 1999-2025 by AfterDawn Ltd.

  IDG TechNetwork