Solved

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

Posted on 2016-11-25
10
128 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 84

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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

679 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