Computer Hope

Software => Computer programming => Topic started by: smilingloki on October 29, 2017, 09:42:10 PM

Title: Bat file, rename part of the file - it won't execute from task scheduler
Post by: smilingloki on October 29, 2017, 09:42:10 PM
Hi everyone,

I have a windows 12 server,

it downloads certain files, I have two problems, hopefully someone can help.

I want to remove, "[eztv]" and i also want to remove,  "WEB" from the files before they are sent to another drive.

the code i use - is this,

@echo off
setlocal enabledelayedexpansion
set deletestring=[eztv]
echo.

for /f "delims==" %%F in ('dir /b ^| find "%deletestring%"') do (
   set oldfilename=%%F
   set newfilename=!oldfilename:%deletestring%=!
   Ren "!oldfilename!" "!newfilename!"
   )

The problem is, i saved this as a .bat file and am running it from the same folder that the files are in - that works fine, it renames the file for the "[eztv]"

Question, how do i include the "WEB" in to the above,

Question, i have it set up correctly in task scheduler, but it shows as "running" and does not change the file name, neither does it stop running in the task scheduler either.
I've checked permissions for the file and they are all fine - can't figure this one out.


I am an absolute newbie at this and any help from you wonderful people would be most appreciated :D

Regards
SL
Title: Re: Bat file, rename part of the file - it won't execute from task scheduler
Post by: Salmon Trout on October 30, 2017, 02:09:06 PM
Try this

@echo off
setlocal enabledelayedexpansion
set deletestring1=[eztv]
set deletestring2=WEB
echo.

for /f "delims==" %%F in ('dir /b ^| find "%deletestring1%"') do (
   set oldfilename=%%F
   set newfilename=!oldfilename:%deletestring1%=!
   Ren "!oldfilename!" "!newfilename!"
   )

for /f "delims==" %%F in ('dir /b ^| find "%deletestring2%"') do (
   set oldfilename=%%F
   set newfilename=!oldfilename:%deletestring2%=!
   Ren "!oldfilename!" "!newfilename!"
   )


Title: Re: Bat file, rename part of the file - it won't execute from task scheduler
Post by: Geek-9pm on October 30, 2017, 04:51:27 PM
Use of ! in place of % is documented here:
https://www.computerhope.com/sethlp.htm   8)

Find the place whee is says:
Quote
Delayed environment variable expansion allows you to use a different character (the exclamation mark) to expand environment variables at execution time. If delayed variable expansion is enabled, the above examples could be written as follows to work as intended:
Code: [Select]
set VAR=before
if "%VAR%" == "before" (
set VAR=after
if "!VAR!" == "after" @echo If you see this, it worked
)

set LIST=
for %i in (*) do set LIST=!LIST! %i
echo %LIST%
Title: Re: Bat file, rename part of the file - it won't execute from task scheduler
Post by: smilingloki on October 30, 2017, 06:59:37 PM
Try this

@echo off
setlocal enabledelayedexpansion
set deletestring1=[eztv]
set deletestring2=WEB
echo.

for /f "delims==" %%F in ('dir /b ^| find "%deletestring1%"') do (
   set oldfilename=%%F
   set newfilename=!oldfilename:%deletestring1%=!
   Ren "!oldfilename!" "!newfilename!"
   )

for /f "delims==" %%F in ('dir /b ^| find "%deletestring2%"') do (
   set oldfilename=%%F
   set newfilename=!oldfilename:%deletestring2%=!
   Ren "!oldfilename!" "!newfilename!"
   )



Thanks Salmon - that works for editing the files, but when i use the task scheduler - for some reason it does not work?
it says that it has run, but it just doesnt edit the file and take out the  [eztv]

Is there something wrong with the file when it runs the .bat  by the task scheduler?

Regards

SL