-1

I need to get the info on the file for when it was last modified using a perl script heres the relevant code:

#where $file equals "MyFile.txt"
$backslash = "\\";
$upTwoLayersHelper = "..$backslash..$backslash";
@fileInfo = qx(forfiles /M $file /P $upTwoLayersHelper /C "cmd /c echo @fdate @ftime");

qx is a function in perl that allows the execution of a system command and redirects the output to a variable

Ive already looked into using stat and it kept returning

Dec 31 of like 1960

so this did not work for me :/

I used the variables $backslash and $upTwoLayersHelper to build 'safely' the path since windows likes to use \ rather than / and I tested to make sure that the variables interpolate within the qx(...here...) function call.

My problem is the only output I get is

ECHO is on.

when I print the @fileInfo variable

BUT when I run the same command within the qx(...) function call outside of perl, and just in batch from the command line as a command it works and returns the correct date information

what am I doing wrong in my perl script?

Thanks for your time!

Coty Embry
  • 958
  • 1
  • 11
  • 24
  • Did you try with the [file test](http://perldoc.perl.org/functions/-X.html) for modification time_ -- `$mod_days = -M $file` ? Also, the module [Win32API::File::Time](http://search.cpan.org/~wyant/Win32API-File-Time-0.006/lib/Win32API/File/Time.pm) should help. – zdim Jul 15 '17 at 05:32
  • You do mention `stat`, but just in case: did you use `(stat $file)[9]` for `mtime`? (The one for `ctime` won't work as expected on Windows.) – zdim Jul 15 '17 at 05:39
  • To clarify -- the `-M` uses a `stat` call but what you say about trying it is strange. The `stat` time-related fields are in seconds _since_ epoch, which _starts_ 1970. How do you get a date "_like 1960_" from that? The `-M` returns time in days, far clearer to check. – zdim Jul 15 '17 at 05:49

1 Answers1

3
@fileInfo = qx(for %x in ($file) do echo %~tx);

perhaps?

The cmd command for such purposes is

for %x in (filename) do echo %~tx

(assign the filename to the metavariable %x , then echo that file's [modification] date)

where the % needs to be %% if executed within a batch rather than from the prompt.

ECHO is on. is characteristic of an echo statement with no argument.

I'm not sure what the two directories higher idea is, but if that is where your target file is located, you'd need to nett prefix filename with ..\..\

Perhaps

qx(echo %* whatever)

where whatever is your proposed cmd command will yield what cmd receives after all the escape-character resolutions (%* means "all supplied arguments")

Magoo
  • 77,302
  • 8
  • 62
  • 84