Solved

Make batch file check if file is locked, and open file if not.

Posted on 2016-11-25
10
80 Views
Last Modified: 2016-11-30
Hello Experts
I have this little batch file, and right now it can check if the file is already open and tell you that it is open and you need to try again later.
The thing i need help with, is expanding it, so that if the ren command is successful I need the batch file to open the pdf file.

Original code that works:
@echo off
ren "Test.pdf" "Test.pdf" 2>nul || (
  echo Sorry, but the file is in use.
  echo Please try again later.
)
pause

Open in new window


My take on open if successful - that don't work and I need help with getting to work:
@echo off
ren "Test.pdf" "Test.pdf" 2>nul || (
  echo Sorry, but the file is in use.
  echo Please try again later.
) else (
start "C:\Test\" Test.pdf
pause

Open in new window


Do one of you know how to get it working :) ??
0
Comment
Question by:Dion Christensen
10 Comments
 
LVL 83

Expert Comment

by:oBdA
ID: 41901543
You're missing the closing round bracket.
Try it like this:
@echo off
ren "Test.pdf" "Test.pdf" 2>nul || (
	echo Sorry, but the file is in use.
	echo Please try again later.
) else (
	start "" "C:\Test\Test.pdf"
)
pause

Open in new window

0
 
LVL 9

Expert Comment

by:Tomas Valenta
ID: 41901548
try this one:
  1. @echo off
  2. ren "Test.pdf" "Test.pdf" 2>nul || (
  3.  echo Sorry, but the file is in use.
  4.  echo Please try again later.
  5. ) else (
  6. start C:\Test\Test.pdf
  7. )
  8. pause
0
 
LVL 7

Expert Comment

by:Andy
ID: 41901582
Try this script:
@echo off
cd /d "c:\test"

for %%a in (test.xlsx) do call :next "test.xlsx"
echo done
pause
goto :eof

:next
set "filename=%~1"
2>nul (call;>>"%filename%") && (
  start C:\test\test.xlsx
  goto done
) || (
  echo "%filename%" is in use or is read only!
)
timeout /t 1 /nobreak
:done

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:Dion Christensen
ID: 41901652
@Andy
That works like a dream.. but it failes when I have "spaces" in my file name like "This is a good day.pdf"
Do you have a fix for that ??
0
 
LVL 7

Accepted Solution

by:
Andy earned 500 total points
ID: 41901671
Try this one:
@echo off
cd /d "c:\test"

set "filename="test 1.xlsx""
2>nul (call;>>"%filename%") && (
  start %filename%
  goto done
) || (
  echo "%filename%" is in use or is read only!
pause
goto done
)
timeout /t 1 /nobreak
:done
exit

Open in new window

0
 

Author Comment

by:Dion Christensen
ID: 41906954
@Andy
Thanks for all the help.
The file looks like this now:
@echo off
:: Info
cd /d "C:\Test"

:: Info
for /f "delims=" %%a in ("Test 1.pdf") do call :next "Test 1.pdf"
echo done
pause
goto :eof

:: Info
:next
set "filename=%~1"
2>nul (call;>>"%filename%") && (
  start C:\Test\"%filename%"
) || (
  echo %filename% is in use or is read only!
timeout /t 10
)
exit

Open in new window

This all works, but there is one small thing that i need help changing now.
I want the echo command to take %filename% and remove the file extension if that is posible.
Can this be done ??
0
 
LVL 7

Assisted Solution

by:Andy
Andy earned 500 total points
ID: 41906958
OK try this:

echo %filename:~0,-4% is in use or is read only!

(e.g. -4 for .pdf -5 for .xlsm)
0
 

Author Comment

by:Dion Christensen
ID: 41906960
@Andy
Amazing... Thank you
0
 
LVL 7

Expert Comment

by:Andy
ID: 41906961
My pleasure! @Dion
0
 

Author Closing Comment

by:Dion Christensen
ID: 41906963
Simple amazing help from @Andy
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question