[SOLVED] Unable to rework working script due to name of folder and / or removal of sign -

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

[SOLVED] Unable to rework working script due to name of folder and / or removal of sign -

#1 Post by DOSadnie » 17 Mar 2023 12:58

This code works A-OK

Code: Select all

@echo off

setlocal enabledelayedexpansion

for /f "tokens=1-3 delims=/" %%A in ('powershell -command "Get-Date -Format 'yyyy/MM/dd'"') do (
  set "year=%%A"
  set "month=%%B"
  set "day=%%C"
)

set "output_file=S:\Configs\HotkeyP\settings\%year% %month% %day%.reg"
set "output_file=!output_file:-= !"
set "output_file=!output_file:  =!"

reg export "HKEY_CURRENT_USER\SOFTWARE\Petr Lastovicka\hotkey" "%output_file%"

endlocal
i.e. it export settings of HotkeyP to to a file in form of

Code: Select all

YYYY MM DD.reg
on my S volume holding various settings; e.g. to file like

Code: Select all

2023 03 17.reg

But if I change that one crucial line to either

Code: Select all

set "output_file=C:\Users\YOUR-USER-NAME\Documents\%year% %month% %day%.reg"
or

Code: Select all

set "output_file="%userprofile\Documents\%year% %month% %day%.reg""
then I get no file in my Documents - but also no error. And that is, I reckon, because the scripts replaces the default

Code: Select all

-
signs from filename with pauses - and as a side effect also unwantedly in the path. And as my folder of User is literally named

Code: Select all

YOUR-USER-NAME
thus it gets transformed during execution to non existing

Code: Select all

YOUR USER NAME
But then again- why do I not get an error or prompt about a need of creating new folder [i.e. >>YOUR USER NAME<< as only >>YOUR-USER-NAME<< exists]?



I have tried using double quotes vs. single ones, capitals vs. lower case, running the BAT from volume C instead from S - but the only thing that helps is just changing that line to e.g.

Code: Select all

set "output_file=C:\Temp\%year% %month% %day%.reg"
because changing it to

Code: Select all

set "output_file=C:\Users\Temp\%year% %month% %day%.reg"
does not help



So what is happening here? How can I write a REG file to my Documents system folder with such simplistic code

Code: Select all

reg export "HKEY_CURRENT_USER\SOFTWARE\Petr Lastovicka\hotkey" "C:\Users\YOUR-USER-NAME\Documents\HotkeyP settings.reg"
but I am unable to do similar with the more complex code which works when output in it is changed to ordinary folders on volume C?
Last edited by DOSadnie on 02 Jul 2023 05:59, edited 3 times in total.

Lucky4Me
Posts: 24
Joined: 21 Oct 2020 06:33

Re: Unable to rework working script due to name of folder and / or removal of sign -

#2 Post by Lucky4Me » 18 Mar 2023 06:09

Try it this way,
I put echo 1) ... , in the script, then you can see what the outcome is
and maybe you can see the problem where it breaks?

Sometimes a username with a space in can break things!

Code: Select all

@echo off

setlocal enabledelayedexpansion

for /f "tokens=1-3 delims=/" %%A in ('powershell -command "Get-Date -Format 'yyyy/MM/dd'"') do (
  set "year=%%A"
  set "month=%%B"
  set "day=%%C"
)

set "output_file=S:\Configs\HotkeyP\settings\%year% %month% %day%.reg"
echo 1) !output_file!
set "output_file=!output_file:-= !"
echo 2) !output_file!
set "output_file=!output_file:  =!"
echo 3) !output_file!

echo 4) reg export "HKEY_CURRENT_USER\SOFTWARE\Petr Lastovicka\hotkey" "%output_file%"

endlocal
Change the first set "output_file=S:\Configs\HotkeyP\settings\%year% %month% %day%.reg" and check what happens.
The echo's without problems, can be removed
This helps me alot to solve problems!

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Unable to rework working script due to name of folder and / or removal of sign -

#3 Post by DOSadnie » 18 Mar 2023 11:25

Lucky4Me wrote:
18 Mar 2023 06:09
Try it this way
[...]
Nothing is happening when I run your code - and thus I did not get to
Lucky4Me wrote:
18 Mar 2023 06:09
Change the first set "output_file=S:\Configs\HotkeyP\settings\%year% %month% %day%.reg" and check what happens.
[...]
When I was coming up with this code of mine I was often getting such zero results when executing a newer version od BAT. [And it also took me many tryouts to get rid of >>-<< signs and extra pauses from the outputted REG file]


It is only afer removing off

Code: Select all

@echo off
[as you were encouraging me to do - assuming I understood you correctly] do I get to see a brief flash of CMD window with e.g.

Code: Select all

C:\q\! SCRIPTS>setlocal enabledelayedexpansion

C:\q\! SCRIPTS>for /F "tokens=1-3 delims=/" %A in ('powershell -command "Get-Date -Format 'yyyy/MM/dd'"') do (
set "year=%A"
 set "month=%B"
 set "day=%C"
)

Lucky4Me
Posts: 24
Joined: 21 Oct 2020 06:33

Re: Unable to rework working script due to name of folder and / or removal of sign -

#4 Post by Lucky4Me » 18 Mar 2023 11:41

C:\q\! SCRIPTS>
is this your working path?

make a working path "c:\q\Scripts" and try your batch again in this directory.

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Unable to rework working script due to name of folder and / or removal of sign -

#5 Post by DOSadnie » 21 Mar 2023 16:05

You mean to put my BAT file in

C:\q\! SCRIPTS\

and change that line in code accordingly - thus execute that code in such version:

Code: Select all

setlocal enabledelayedexpansion

for /f "tokens=1-3 delims=/" %%A in ('powershell -command "Get-Date -Format 'yyyy/MM/dd'"') do (
  set "year=%%A"
  set "month=%%B"
  set "day=%%C"
)

set "output_file=C:\q\! SCRIPTS\%year% %month% %day%.reg"
set "output_file=!output_file:-= !"
set "output_file=!output_file:  =!"

reg export "HKEY_CURRENT_USER\SOFTWARE\Petr Lastovicka\hotkey" "%output_file%"

endlocal
?


If yes, then I also get no REG file, just a flashed CommandLine window with

Code: Select all

C:\q\! SCRIPTS>setlocal enabledelayedexpansion

C:\q\! SCRIPTS>for /F "tokens=1-3 delims=/" %A in ('powershell -command "Get-Date -Format 'yyyy/MM/dd'"') do (
set "year=%A"
 set "month=%B"
 set "day=%C"
)

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

Re: Unable to rework working script due to name of folder and / or removal of sign -

#6 Post by ShadowThief » 21 Mar 2023 21:13

Wait, you actually have an exclamation point followed by a space in the name of your folder? Those are the two worst choices you could possibly make. Immediately rename the folder to just Scripts. Because you have delayed expansion enabled, the exclamation point has special meaning. You can try to escape the one in the path, but honestly it will go much more smoothly if you name your folders correctly.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Unable to rework working script due to name of folder and / or removal of sign -

#7 Post by Compo » 22 Mar 2023 05:06

To begin with, there is no need whatsoever, in your script, to be using delayed expansion.
Next, as you're generating a date in a very specific format, there is no need to include code to replace all hyphens with spaces, and then remove all double spaces.
All you need to do is to is to generate the exact filename string you need:

Code: Select all

@SetLocal EnableExtensions DisableDelayedExpansion
@Set "OutputFilePath=S:\Configs\HotkeyP\settings"
@For /F "Delims=" %%G In ('%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile "'%OutputFilePath%\' + $(Get-Date).ToString('yyyy MM dd') + '.reg'"') Do @%SystemRoot%\System32\reg.exe Export "HKCU\SOFTWARE\Petr Lastovicka\hotkey" "%%G"
It doesn't matter if you define the initial variable as C:\q\! SCRIPTS, C:\Users\YOUR-USER-NAME\Documents, or %UserProfile%\Documents, the code should work without issue.

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Unable to rework working script due to name of folder and / or removal of sign -

#8 Post by DOSadnie » 01 Jul 2023 12:03

And it seems to work

Thus I can use this

Code: Select all

@SetLocal EnableExtensions DisableDelayedExpansion

@Set "OutputFilePath=%userprofile%\Documents\"

@For /F "Delims=" %%G In ('%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile "'%OutputFilePath%\' + $(Get-Date).ToString('yyyy MM dd') + '.reg'"') Do @%SystemRoot%\System32\reg.exe Export "HKEY_CURRENT_USER\SOFTWARE\Petr Lastovicka\hotkey" "%%G"

to get that part of my Registry exported to a file bearing current date


Thank you very much for solving this issue and explaining it

Post Reply