Trying to close files on a server, to process them

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
cabignon@batibig.com
Posts: 10
Joined: 01 Apr 2016 02:34

Trying to close files on a server, to process them

#1 Post by cabignon@batibig.com » 01 Apr 2016 02:49

Hello,

I need to create a batch to move pdf files from a directory to a sub directory.
But those pdf files are open on a shared drive and cannot be moved.
So first I need them to be closed.

I am fully newbie in batch commands and I found the move command and my lines are working for all "not oppened" files but not for the openones.

So I found the Close command and the findstr command but something is wrong in my code can you help please ?

Code: Select all

findstr /I OS \\Server\CHAPEAU\OS\*.pdf
for /F %%a in (findstr /I OS \\Server\CHAPEAU\OS\*.pdf) do net files %%a /close
move /y "\\Server\CHAPEAU\OS\*.*" "\\Server\CHAPEAU\OS\Archive OS"



THANK'S A LOT !
Charles From PARIS FRANCE

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: HELP wtih a batch file needed

#2 Post by foxidrive » 01 Apr 2016 06:24

Test this:


Code: Select all

set "server=\\Server\CHAPEAU\OS"
for /F "delims=" %%a in ('findstr /I /M OS "%server%\*.pdf"') do echo "%server%\%%a" & net files "%server%\%%a" /close
move /y "%server%\*.*" "%server%\Archive OS"

cabignon@batibig.com
Posts: 10
Joined: 01 Apr 2016 02:34

Re: HELP wtih a batch file needed

#3 Post by cabignon@batibig.com » 01 Apr 2016 06:58

Thank's but it does not close the open files in \\server\chapeau\os
So I still have the error message close all files...

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: HELP wtih a batch file needed

#4 Post by foxidrive » 01 Apr 2016 07:09

I didn't say the /close switch would work, you said that. :D

cabignon@batibig.com
Posts: 10
Joined: 01 Apr 2016 02:34

Re: HELP wtih a batch file needed

#5 Post by cabignon@batibig.com » 01 Apr 2016 07:21

the /close switch is working from the prompt windows on the Server with the "number" of the file like :
net file 933 /close
but I need the *.pdf instead of the number because I never know the number of the opened files...

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: HELP wtih a batch file needed

#6 Post by aGerman » 01 Apr 2016 09:50

Charles

Does the NET FILE command (without ID and /close) list the opened files? In that case there would be a good chance to find and extract the file ID that you can use with the /close option.

Regards
aGerman

cabignon@batibig.com
Posts: 10
Joined: 01 Apr 2016 02:34

Re: HELP wtih a batch file needed

#7 Post by cabignon@batibig.com » 01 Apr 2016 10:16

yes sir and others
I mean all the files open on the D: (not only the D:\CHAPEAU\OS like I want)
If I do on prompt : cd CHAPEAU\OS
I have :
D:\CHAPEAU\OS>net files
Will show all the open files even those at the root of D:\

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: HELP wtih a batch file needed

#8 Post by aGerman » 01 Apr 2016 10:33

I can't test it because NET FILE doesn't output anything on my netbook.

Code: Select all

@echo off &setlocal
set "server=\\Server\CHAPEAU\OS"
for /F "delims=" %%a in ('findstr /I /M OS "%server%\*.pdf"') do (
  echo "%server%\%%a"
  move /y "%server%\%%a" "%server%\Archive OS\" || (
    for /f %%b in ('net file^|findstr /ec:"\\%%a"') do (
      net file %%b /close
      timeout /t 2 /nobreak
      move /y "%server%\%%a" "%server%\Archive OS\"
    )
  )
)
pause

cabignon@batibig.com
Posts: 10
Joined: 01 Apr 2016 02:34

Re: HELP wtih a batch file needed

#9 Post by cabignon@batibig.com » 01 Apr 2016 11:03

ok I am not a the office anymore
thank's I ll let you know
regards
Bon week end !

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: HELP wtih a batch file needed

#10 Post by ShadowThief » 01 Apr 2016 12:47

Is it net files? I thought it was just net file.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: HELP wtih a batch file needed

#11 Post by aGerman » 02 Apr 2016 04:22

just net file

Exactly that. I corrected my c/p failure. Thanks!

cabignon@batibig.com
Posts: 10
Joined: 01 Apr 2016 02:34

Re: HELP wtih a batch file needed

#12 Post by cabignon@batibig.com » 04 Apr 2016 03:51

Hello
Thank's for your help but i guess It finds the files but after it cannot move it because the path is double :

"\\Server\CHAPEAU\OS\\\Server\CHAPEAU\OS\OSC-2016-04-1458.pdf"
Le chemin d'accès spécifié est introuvable

etc....

Can you still help please ?

Thx
C.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: HELP wtih a batch file needed

#13 Post by aGerman » 04 Apr 2016 04:54

I guess it happens because of the %sever%\ in the source path for the MOVE command.
Try

Code: Select all

      move /y "%%a" "%server%\Archive OS\"

instead.

cabignon@batibig.com
Posts: 10
Joined: 01 Apr 2016 02:34

Re: HELP wtih a batch file needed

#14 Post by cabignon@batibig.com » 04 Apr 2016 06:21

hello
it is working on the not open files but it is not closing the files opened.

I guess the net file /close is not working it says : le processus ne peut accéder au fichier car ce fichier est utilisé par un autre processus

The process can't access the file because the file is used by another process (something like that)

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: HELP wtih a batch file needed

#15 Post by aGerman » 04 Apr 2016 07:06

Two things that you may try:
1) If not already done, please run the script as aministrator (via right-click) to get elevated permissions.
2) Maybe the system had not enough time to close the file before the script continues. Try to increase the timeout from 2 seconds to 5 or 10.

Post Reply