-1
import sys, os, datetime, time, urllib
from subprocess import *

List_SP_Servers=["toto1","toto2","toto3"]
ListTraceFile_SP_Servers=["c:\\\\dic1\\\\udic1\\\\file1.txt","c:\\\\dic1\\\\udic1\\\\file2.txt"]

for l__sp in List_SP_Servers:
    try:
        print '\n--------'+str(l__sp)+'--------'
        for l__traces in ListTraceFile_SP_Servers:
            #l__cmd1 = '['+'\"psexec\",\"\\\\'+l__sp+'\",'+'\"-nobanner\"'+',\"ls\",\"-al\"'+',"'+l__traces+'\"'+']'
            #l__cmd1 = '(['+'\"psexec\",\"\\\\'+l__sp+'\",'+'\"-nobanner\"'+',\"ls\",\"-al\"'+',"'+l__traces+'\"'+'],shell=True,stdout=PIPE).communicate()[0]'
            #print l__cmd1
            #output1=popen(l__cmd1,shell=True,stdout=PIPE).communicate()[0]
            #l__cmd1 = "[\"C:\\Tools\\Sysinternalsuite\\psexec\",\"\\\\\\"%s"\",\"-nobanner\",\"ls\",\"-al\","%s"\"]" % (l__sp,l__traces)
            #print 'La vies est -->'+l__cmd1
            #l__cmd1_WithCall = "\'psexec\",\"\\\\\\"%s"\",\"-nobanner\",\"ls\",\"-al\" "%s"\'" % (l__sp,l__traces)
            l__cmd1_WithCall = "\'psexec \\\%s -nobanner ls -al %s\'" % (l__sp,l__traces)
            #l__cmd1_WithCall = "\"psexec\",\"\\\%s,\"ls\",\"-al\",%s" % (l__sp,l__traces)
            print 'l__cmd1_WithCall-->',l__cmd1_WithCall
            p5=call(l__cmd1_WithCall,shell=True)
            print '---->',p5,'<-----'
    except:
        print '\n'+l__sp+' Can\'t reach it !\n'

The above code gives me error as I am trying to get the last modified date of some files from a Windows server to a remote Windows server. I am on Windows using psexec but maybe they are other solutions to get such information.

I tried using subprocess.call, subprocess.popen, subprocess.check_out and all give me errors, but when I tried to type directly the command on cmd it is working fine.

Praveen
  • 8,945
  • 4
  • 31
  • 49
  • Please also give us the error message and at which line in your code snippet it occurs. Also, have you considered retrieving the times using [`os.stat()`](https://docs.python.org/3/library/os.html#os.stat) or similar from the Python standard library instead of going through external tools? – blubberdiblub May 01 '17 at 06:03
  • Thanks. This is my issue. I know how to get the information when locally. My goal is to get the same for files in remote servers. Also, if you see the commands inside l_cmd1 it is working fine when I type it in windows command prompt but while place it in subprocess.popen it is not. And you are right I am sure there is another way by not using external tools, but how? – vivadeen May 01 '17 at 06:47
  • You still haven't shown us the error you're getting. – blubberdiblub May 01 '17 at 06:51
  • --------toto1-------- l__cmd1_WithCall--> 'psexec \\toto1 -nobanner ls -al c:\\dic1\\udic1\\file1.txt' ''psexec' is not recognized as an internal or external command, operable program or batch file. ----> 1 <----- l__cmd1_WithCall--> 'psexec \\toto1 -nobanner ls -al c:\\dic1\\udic1\\file2.txt' ''psexec' is not recognized as an internal or external command, operable program or batch file. ----> 1 <----- --------toto2-------- l__cmd1_WithCall--> 'psexec \\toto2 -nobanner ls -al c:\\dic1\\udic1\\file1.txt' ''psexec' is not recognized as an internal or external command, operable prog – vivadeen May 01 '17 at 07:11
  • @vivideen This should go into the question description (you can edit it), as it's an important part for understanding the question. At first glance, it looks like your quoting is wrong - you need to remove the single quotes around the whole command line, as it's trying to execute a command with the name `psexec \\toto1 -nobanner ...` and so on, instead of a command `psexec` and separate arguments. – blubberdiblub May 01 '17 at 07:52
  • Thanks blubberdiblub. I tried many changes but could not get it. Therefore I tried the solution of Stavros Avramidis which worked and seems to me quicker than using the external tool psexec. Maybe they are others ways and quicker but at least it is working. Thanks again. – vivadeen May 02 '17 at 06:19

1 Answers1

2

In order to get the last time a file was modified in windows ,you can use the code below

import os,datetime
timestamp = os.stat(PATH_OF_FILE).st_ctime
readable = datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')

first you get the time stamp and readable converts it into date

  • Great. But my concern as shown in the code is remotely. This works fine and I got it when you are in the same server (locally) but I would like the same for files in remote server. – vivadeen May 01 '17 at 06:35
  • @vivadeen You could make the relevant directories available as shares. I mean you allow PsExec, which is even more powerful, so what speaks against a share? – blubberdiblub May 01 '17 at 06:55
  • Ok for this solution if you have only one or 2 remote servers to read from but I have 70 remote servers to get this information. – vivadeen May 01 '17 at 07:13
  • You can mount the network drive as described in this question http://stackoverflow.com/a/19171820/5306132 – Stavros Avramidis May 01 '17 at 09:06
  • Thanks Stavros Avramidis, good idea which I used. It is at least working quicker than using psexec, maybe they are others ways and quicker but at least it is working. Thanks again. – vivadeen May 02 '17 at 06:20