Solved

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

Posted on 2016-11-25
10
175 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

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 …
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

751 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