LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-02-2011, 07:20 AM   #1
niiiro
Member
 
Registered: Feb 2010
Posts: 75

Rep: Reputation: 1
Dos programming - Directories


Hey Guys,

I'm writing a small script in Batch and I would like to code something that it will enter various directories, do some actions, leave directory and enter the next one. these directories names change often so I can't set permanent names.

will appreciate your kind help.


Regards,

Nir.
 
Old 01-02-2011, 07:54 AM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by niiiro View Post
will appreciate your kind help.
Sure. We're always glad when we can help.
 
Old 01-02-2011, 08:04 AM   #3
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,281

Rep: Reputation: 320Reputation: 320Reputation: 320Reputation: 320
Just store these directory names in other file. You only need to choose best format for your needs. The easiest is to write them as bash variable, separating by IFS environment variable:
Code:
DIRECTORIES='/tmp/dir1 /home/dir2 /usr/bin/dir3'
and saving file for example "directories.conf"
and then in the script you refer it:
Code:
#!/bin/bash

DIRECTORIES=''

source ./directories.conf

for DIR in $DIRECTORIES ; do
    echo "Processing: $DIR"
done
 
Old 01-02-2011, 08:16 AM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by crts View Post
Sure. We're always glad when we can help.
Was there any reason to remove this thread from the zero replies list?

eSelix, I believe the OP wants to do this in a DOS batch script (i.e. under Windows), hence the title of their post..
 
Old 01-02-2011, 08:30 AM   #5
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by Nylex View Post
Was there any reason to remove this thread from the zero replies list?
Yes, since there was no real question I assume that the OP will come up with one in the near future. At least this is how I interpret
Quote:
will appreciate your kind help
And since the OP is asking a DOS question - and it has happened that similar requests were (falsely) 'shot' down in the past just because not being Linux related - I thought it was necessary to reaffirm the OP that his question is valid nonetheless and in the appropriate forum.

@OP: If you actually do have a question at this point then please post what you have tried so far and where you are stuck. Then we can see if we can help you solve the issue.
 
1 members found this post helpful.
Old 01-02-2011, 09:09 AM   #6
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,631

Rep: Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696
DOS

Actually, there may be a way to parse the output of the dir command to get your list of directories. It would be simple if they were all subdirectories of the folder, and that folder contained no files or shortcuts.

We have to little information to determine an optimal way to help. Can you provide more detail?
 
Old 01-02-2011, 09:16 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by niiiro View Post
I'm writing a small script in Batch and I would like to code something that it will enter various directories, do some actions, leave directory and enter the next one. these directories names change often so I can't set permanent names.
How can the script know which directories to enter?
 
Old 01-02-2011, 12:27 PM   #8
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Just out of curiosity, why aren't you doing the script in Powershell?

Last edited by dugan; 01-02-2011 at 12:31 PM.
 
Old 01-03-2011, 12:59 AM   #9
niiiro
Member
 
Registered: Feb 2010
Posts: 75

Original Poster
Rep: Reputation: 1
Lots of activity in here..

anyway.. here is the directory tree so you guys get a better view:

Code:
├───CP1 raw data
│   ├───K33786-10-CP1
│   ├───K33787-10-CP1
│   ├───K33789-10-CP1
│   ├───K33895-10-CP1
│   ├───K33897-10-CP1
│   ├───K33903-10-CP1
│   ├───K33933-10-CP1
│   └───KL0985-10-CP1
├───CP1 summary data
├───CP2
│   ├───K33786-10-CP2
│   ├───K33787-10-CP2
│   ├───K33789-10-CP2
│   ├───K33897-10-CP2
│   ├───K33905-10-CP2
│   └───K33933-10-CP2
├───Finish Line
└───WAT
what i'd like to do is as follows:

* While being under "CP1 raw data" directory, the script will enter each directory (those directory names change often and they get their name randomly. just as seen in the directory tree.) and preform a few actions such as extraction and packing. as for these actions - i have a code written already. after preforming these actions, leave the current directory and move to the next one.



Hope that clears things up a little...

eSelix, thanks but thats irreverent since I can't tell what would be the exact directory name in advance.
Dugan, I don't know powershell at all so I didn't even try getting into this.
 
Old 01-03-2011, 07:38 AM   #10
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Code:
@echo off
setlocal

for /r .. %%d in (.) do (
  cd %%d
  REM process here
)
 
Old 01-03-2011, 09:23 AM   #11
niiiro
Member
 
Registered: Feb 2010
Posts: 75

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by ntubski View Post
Code:
@echo off
setlocal

for /r .. %%d in (.) do (
  cd %%d
  REM process here
)
Code:
@echo off
setlocal

for /r .. %%d in (.) do (
  cd %%d
  paext.exe.lnk -p%%d "C:\FTP_Data\Template\CP1 raw data\*.gz"   <--- ignore this line syntax
  REM process here
)
Is my syntax ok...? because it doesn't seem to enter sub-directories.
 
Old 01-03-2011, 08:53 PM   #12
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
ignore this line syntax
...
Is my syntax ok...?
Are you asking if the syntax that you said to ignore is okay?

Quote:
because it doesn't seem to enter sub-directories.
It enters subdirs for me:

Code:
C:\Users\npostavs\tmp\CP1 raw data>tree /f ..
C:\USERS\NPOSTAVS\TMP
│   dodirs.bat
│
├───CP1 raw data
│   ├───K33786-10-CP1
│   ├───K33787-10-CP1
│   └───K33789-10-CP1
├───CP1 summary data
├───CP2
│   ├───K33786-10-CP2
│   ├───K33787-10-CP2
│   └───K33789-10-CP2
├───Finish Line
└───WAT

C:\Users\npostavs\tmp\CP1 raw data>type ..\dodirs.bat
@echo off
setlocal

for /r .. %%d in (.) do (
  cd %%d
  REM cd by itself prints current directory
  cd
)

C:\Users\npostavs\tmp\CP1 raw data>..\dodirs.bat
C:\Users\npostavs\tmp
C:\Users\npostavs\tmp\CP1 raw data
C:\Users\npostavs\tmp\CP1 raw data\K33786-10-CP1
C:\Users\npostavs\tmp\CP1 raw data\K33787-10-CP1
C:\Users\npostavs\tmp\CP1 raw data\K33789-10-CP1
C:\Users\npostavs\tmp\CP1 summary data
C:\Users\npostavs\tmp\CP2
C:\Users\npostavs\tmp\CP2\K33786-10-CP2
C:\Users\npostavs\tmp\CP2\K33787-10-CP2
C:\Users\npostavs\tmp\CP2\K33789-10-CP2
C:\Users\npostavs\tmp\Finish Line
C:\Users\npostavs\tmp\WAT

C:\Users\npostavs\tmp\CP1 raw data>
 
Old 01-03-2011, 10:18 PM   #13
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 490
Blog Entries: 4

Rep: Reputation: 238Reputation: 238Reputation: 238
As an old DOS dinosaur, i have met this kind of problem many times. The problem here is that DOS batch processes are inherently simple, with no built-in ability to perform bash-like logic.

In my opinion, eSelix got you started in the right direction by telling you to store the directory names in a text file. Change the text file every time the directory names change. But that's tedious at best. So let me ask you: do you have a DOS based text editor that includes some sort of macro capabilities? (My favorite DOS text editor was QEdit by Semware.) If you have something like that, here's what you do:

Pipe the DIR command into a text file
Code:
DIR *.* >> mybatch.bat
Then use the text editor's macro to change each line into the appropriate command using the directory name on that line as its argument. Run it as a batch file, and you're done.

If you don't have a macro-capable text editor, let me know, and we'll take it from there.
 
Old 01-04-2011, 05:23 AM   #14
niiiro
Member
 
Registered: Feb 2010
Posts: 75

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by ntubski View Post
Are you asking if the syntax that you said to ignore is okay?



It enters subdirs for me:

Code:
C:\Users\npostavs\tmp\CP1 raw data>tree /f ..
C:\USERS\NPOSTAVS\TMP
│   dodirs.bat
│
├───CP1 raw data
│   ├───K33786-10-CP1
│   ├───K33787-10-CP1
│   └───K33789-10-CP1
├───CP1 summary data
├───CP2
│   ├───K33786-10-CP2
│   ├───K33787-10-CP2
│   └───K33789-10-CP2
├───Finish Line
└───WAT

C:\Users\npostavs\tmp\CP1 raw data>type ..\dodirs.bat
@echo off
setlocal

for /r .. %%d in (.) do (
  cd %%d
  REM cd by itself prints current directory
  cd
)

C:\Users\npostavs\tmp\CP1 raw data>..\dodirs.bat
C:\Users\npostavs\tmp
C:\Users\npostavs\tmp\CP1 raw data
C:\Users\npostavs\tmp\CP1 raw data\K33786-10-CP1
C:\Users\npostavs\tmp\CP1 raw data\K33787-10-CP1
C:\Users\npostavs\tmp\CP1 raw data\K33789-10-CP1
C:\Users\npostavs\tmp\CP1 summary data
C:\Users\npostavs\tmp\CP2
C:\Users\npostavs\tmp\CP2\K33786-10-CP2
C:\Users\npostavs\tmp\CP2\K33787-10-CP2
C:\Users\npostavs\tmp\CP2\K33789-10-CP2
C:\Users\npostavs\tmp\Finish Line
C:\Users\npostavs\tmp\WAT

C:\Users\npostavs\tmp\CP1 raw data>

it works perfectly!

Thanks guys!
 
Old 01-04-2011, 01:04 PM   #15
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by dogpatch View Post
My favorite DOS text editor was QEdit by Semware.
But, I thought you said you were a DOS Dinosaur...
I would still like to have a few features from that editor in a simple *nix text-mode editor. Best editor, ever.

I used to use a package of Unix-like tools, including a Bourne shell-ish, that would provide much of the functionality that we take for granted in Linux these days, but in DOS. I remember it was a commercial package, but I seem to recall that there was also a free version, or perhaps the commercial product went 'free' when it neared end-of-life. I cannot remember the name of it. Maybe the OP can still find such a toolkit to solve the problem.

I think I'll dig out some of my 20 year old Dr. Dobbs mags, and see if I can find one of their advertisements.

--- rod.

Last edited by theNbomr; 01-04-2011 at 01:14 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
DOS question: KEY programming DavidMcCann General 5 04-01-2010 04:47 PM
programming with directories juanbobo Programming 9 03-28-2005 01:09 PM
How to change directories in DOS zWaR General 17 11-09-2004 10:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration