Jump to content

poly4life

Member
  • Posts

    10
  • Joined

  • Last visited

  • Donations

    0.00 USD 
  • Country

    United States

About poly4life

Profile Information

  • OS
    XP Pro x86

poly4life's Achievements

0

Reputation

  1. I would LOVE to use just one tool to get the job done. But I am just the messenger. The powers that be want it in a batch file. It is crazy, but sometimes, as a coder, you have to hack things together to get the job done under these types of situations (working for others using strange premises). If this was for me and/or I was given more freedom and time, I would do things much differently. The reason I brought up calling some other langauge, like perl, with these batches, is one of the tasks I am doing is reading in an excel file. vbs has support for excel, but I had already started using perl earlier in this task and it's too late for me to go back and rewrite perl code into vbs. Besides, the perl modules for manipulating excel files is pretty good and sufficient enough for reading it in. The excel file will essentially be treated like a flat database, (i.e. A1 correlates to B1, A2 correlates to B2, and so on, until EOF). There's some "garbage" data in the excel file I have to filter out, so it's not exactly 1-1 but that's not difficult). So, I'll loop through each cell mappings, line by line, and use those cell values to perform some other processing. Again, if this was my choice, I'd use some other construct.
  2. Well,they say one of perl's typical uses is "gluing" code together. But what I would say to use one or the other is these are my orders; I'm just the messenger. Could you please elaborate on your first suggestion? Like this: @echo off FOR /F "delims=" %%I IN ('perl.exe -e "my $string='Hello World'; print STDOUT $string;"') DO set var=%%I echo %var% I tested it and it looks like it captures it--I'm thinking it's using batch's STDOUT stream or CONSOLE stream. In general, this would be useful for straight batch files, no? To have the ability to capture output from batch's STDOUT or CONSOLE stream by employing batch's STDIN stream. I tried that before using ">","<", and "|" characters but I had no luck. Thank you.
  3. Hello, Is there a way I can pass the output of a Perl script to a batch file? I'm running a batch and I call a perl script from within that batch. Inside the Perl script, I set a variable. I want to capture said variable as output and and input it into a batch variable. Not really pseudo code, just conceptual: i..e REM I'm in a batch file SET Batch_Input_Var = Perl.exe Capture_Perl_Output.pl; To put it another way (not any particular language): Function Foo() { Output = 1; return Output; } Input = Foo(); print Input; // 1 Thank you.
  4. Yes, thank you, I see that now, "+>" is not the right tool for the job. However, I was looking at overwrite, as opposed to append, because I didn't wish to append data to the end of the file. FYI -- I should've mentioned in this in the original post -- I am running Strawberry Perl 5.12.2.0 on Windows XP SP3 32-bit, and the file I'm opening is a text file and some of the processing involves regex. After processing, I rewrite the entire file, instead of just a portion of it. The problem is the much of the rewrite starts at the beginning of the file. Again, my logic was instead of picking-and-choosing which parts of the file to rewrite, why not just read the entire contents (it's not a huge file) of the text file into Perl, do processing on it, then rewrite the entire file. I looked at the seek doc and did some more research on append and seek, and at least from unix/linux, it is not possible to syseek or seek with append. I had tried it, too, and it would only append to the end of the file. SOURCE: http://www.justlinux.com/forum/showthread.php?t=131467 MY CODE for read/append ("+>>") #!/usr/local/bin/perl -w use Fcntl qw(:DEFAULT :flock :seek); # import LOCK_* constants open(FILE, "+>>", "test.txt") || die("Cannot open file"); flock(FILE, LOCK_EX); seek(FILE, 0, SEEK_SET); $file_data=<FILE>; print $file_data; print FILE "xxx"; close(FILE); print "\n\n-------\n\n"; Beforehand, I opened the file in read-mode, copied its contents, closed the file, opened the file again in write-overwrite-mode, wrote to the file, and, finally, closed the file. But I thought why open and close the same file twice, when I may be able to do it all in one shot? It'll be more efficient, less code, making it potentially easier to maintain and debug, and less of a performance hit. With one file, it's no big deal. But if I'm processing many, many files (i.e. reading in a directory), I could see a performance hit. So, this is why I posted in the first place, to learn if there's a better way. I like the suggestion for the backup, thank you. I suppose I'll just open it twice, unless I can get the Tie::File module to work correctly. Thank you both again.
  5. Hello, I'm having difficulty reading from and writing to the same file. I'm not sure what I'm doing wrong. It works fine if I'm in read/write append mode, but that doesn't accomplish what I want. I'm trying to read the file in, do some processing on it, then overwrite the file. I honestly don't understand the purpose of "+> operator". "+<" works fine, but it appends to the file. "+>" deletes the contents of the file immediately after I open it. Is it that the "+>" operator is simply not the right tool for reading from and (over)writing to the same file? I saw a perl module called Tie::File but I am having difficulty using it and would appreciate some help with it. Otherwise, I'll just read from the file, close it, and then write to the file, if I can't get the "+>" operator working for this problem. Thank you. Here's my code: #!/usr/local/bin/perl -w use Fcntl qw(:DEFAULT :flock :seek); # import LOCK_* constants local $/=undef; #Read/Write with overwrite open(FILE, "+>", $file) || die("Cannot open file"); flock(FILE, LOCK_EX); seek(FILE, 0, SEEK_SET); $file_data=<FILE>; #Do some processing on $file_data here # (Over)Write to same file print FILE $file_data; close(FILE); Thank you.
  6. Thank you both. I like that nircmd program. I think I'll look into porting over the batch code to powershell or vbs, instead of "putting the square peg in the round hole". Thanks again.
  7. Because there may be a better way to do something, I ask the question. this is not a forum to rant, but memory is a luxury and code, in general, is not as tight as it was when memory was scarce. I'm not an expert, but I always want to learn and I'm willing to abandon my method if there is a better way. Thank you.
  8. Hello, If you're familiar with Perl, it has a feature where you can run it in single line edit mode, which allows you to write the code out all in one line, instead of calling my_script.vbs, for example. Is there a way I can just execute code in a similar single line edit mode, to avoid calling an external file? I just want to write out a msgbox that displays to the user from a batch, and it seems unnecessary to create another file which just has "msgbox...". I'm open to other suggestions, if there's a way to have a pop-up window display to the user directly from a batch without using an external language like vbscript. Thank you. Update: I did find this site that has been of great help --> http://www.robvanderwoude.com/usermessages.php I took this bit of code here: > usermessage.vbs ECHO WScript.Echoˆ( "Multiple lines?" ˆ& vbCrLf ˆ& "OK..." ˆ) WSCRIPT.EXE usermessage.vbs DEL usermessage.vbs And it works fine, but I was hoping to do something like this (pseudo-code): WSCRIPT.EXE "WSCRIPT.Echo('This is pseudo code, to show I want to display a msgbox without calling an external script.')"
  9. Hello, As the Topic Title says, I want to copy multiple files to multiple directories, all in one line. The files doesn't necessarily have the same extensions and the directories are not necessarily similar. Example: I want to copy Test.txt, Test.html, Test.doc, Test.xls, and AnotherTest.txt to folders C:\Root\Documents, C:\Root\Folder and C:\Root\MoreDocuments. I don't think there's a way to do this with a straight copy or xcopy command, correct? I mean, if I was using some form of wildcard for the file or folders, then maybe I might have something (i.e. copy *.txt C:\Root\Folder and/or an Exclusions list). If I'm copying a file to multiple folders, or multiple files to a folder, or -- in my case -- multiple files to multiple folders, I probably have to use a FOR loop, correct? The goal is to get it all in one line. I thought, perhaps, I can use nested FOR loops to accomplish this, but I'm getting an error of "The syntax of the command is incorrect." Here's the code(I am executing this from a batch file, NOT the command line prompt): FOR %%X in (Test.txt Test.html Test.doc Test.xls AnotherTest.txt) DO (FOR %%G in (Documents Folder MoreDocuments) DO COPY %%X %%G) Why is this not working? I would appreciate any help or improvements. Thank you. Update: Somewhat strange -- I had to move the opening parenthesis to the first line, probably because of a carriage return. Now the nested for loop works. Still, I would be interested if there are other methods, better methods, perhaps one that just involves copy or xcopy. Thank you.
×
×
  • Create New...