Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variables in Windows Commands 1

Status
Not open for further replies.

crofty13

Technical User
Oct 26, 2007
1
GB
Im a complete neebee so my apologies for this:

How can I put variables in commands that I give to the OS?

my @filelist =`dir /OD $dir`; #( /OD option puts them in order.)

I use the above from this script below but get the error: dir: cannot access /OD: No such file or directory

Any ideas?


#!C:\strawberry-perl\perl\bin\perl.exe

use strict;
use File::Copy;
use File::path;

my $dir ='C:\test';
my @filelist =`dir /OD $dir`;

print $filelist[5];
 
Works fine for me using activestate and your code looks fine. What windows are you using?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
First time I've seen someone using strawberry perl:

#!C:\strawberry-perl\perl\bin\perl.exe

I'm thinking of trying it myself, just need to download it but it's really slow on dial-up.

Try putting your option switches at the end:

my @filelist =`dir $dir /OD`;





------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Taking the OS out of the equation:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]File::Spec::Functions[/green] [red]qw([/red][purple]catfile[/purple][red])[/red][red];[/red]

[black][b]use[/b][/black] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$dir[/blue] = [red]'[/red][purple]C:/test[/purple][red]'[/red][red];[/red]

[url=http://perldoc.perl.org/functions/opendir.html][black][b]opendir[/b][/black][/url][red]([/red]DIR, [blue]$dir[/blue][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't open [blue]$dir[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[black][b]my[/b][/black] [blue]@files[/blue] = [url=http://perldoc.perl.org/functions/grep.html][black][b]grep[/b][/black][/url] [red]{[/red]![red]/[/red][purple]^[purple][b]\.[/b][/purple]+$[/purple][red]/[/red][red]}[/red] [url=http://perldoc.perl.org/functions/readdir.html][black][b]readdir[/b][/black][/url][red]([/red]DIR[red])[/red][red];[/red]
[url=http://perldoc.perl.org/functions/closedir.html][black][b]closedir[/b][/black][/url][red]([/red]DIR[red])[/red][red];[/red]

[gray][i]# Sort using Schwartzian transform[/i][/gray]
[blue]@files[/blue] = [url=http://perldoc.perl.org/functions/map.html][black][b]map[/b][/black][/url] [red]{[/red] [blue]$_[/blue]->[red][[/red][fuchsia]0[/fuchsia][red]][/red] [red]}[/red]
	[url=http://perldoc.perl.org/functions/sort.html][black][b]sort[/b][/black][/url] [red]{[/red] [blue]$a[/blue]->[red][[/red][fuchsia]1[/fuchsia][red]][/red] <=> [blue]$b[/blue]->[red][[/red][fuchsia]1[/fuchsia][red]][/red] [red]}[/red]
	[black][b]map[/b][/black] [red]{[/red] [red][[/red][blue]$_[/blue], [red]([/red][url=http://perldoc.perl.org/functions/stat.html][black][b]stat[/b][/black][/url][red]([/red][maroon]catfile[/maroon][red]([/red][blue]$dir[/blue], [blue]$_[/blue][red])[/red][red])[/red][red])[/red][red][[/red][fuchsia]9[/fuchsia][red]][/red][red]][/red] [red]}[/red]
	[blue]@files[/blue][red];[/red]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [url=http://perldoc.perl.org/functions/join.html][black][b]join[/b][/black][/url] [red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red], [blue]@files[/blue][red];[/red]

[fuchsia]1[/fuchsia][red];[/red]

[teal]__END__[/teal]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Core (perl 5.8.8) Modules used :
[ul]
[li]File::Spec::Functions - portably perform operations on file names[/li]
[/ul]
[/tt]

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top