Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
nickodemos
- Posts: 16
- Joined: 29 Jan 2013 08:17
#1
Post
by nickodemos » 27 Apr 2019 08:05
Below is a script I have been using for some time. Not sure where I got it but I have tried to work in a hard link instead of move command but keep getting errors. To me as a novice it seems the commands would be simple enough to do yet nothing has worked.
Any help would be appreciated.
Code: Select all
@echo off
setlocal
REM store current directory. Using separate variable makes it
REM easier to change behaviour too.
set dir=%cd%
REM call subroutine for each supported extension.
call :do .epub
call :do .mobi
call :do .pdf
call :do .jpg
REM Main program done.
echo Press a key to close.
pause
exit /b
:do
set ext=%1
REM loop through all files with the given extension.
for /f "tokens=*" %%f in ('dir /b /on "%dir%\*%ext%"') do (
REM trim the extension and use the base name as directory name.
setlocal EnableDelayedExpansion
set thefile=%%~nf
echo !thefile!
md "%dir%\!thefile!"
REM move all files that start with the same base name.
move "%dir%\!thefile!*.*" "%dir%\!thefile!\"
)
REM exit subroutine
exit /b
-
Squashman
- Expert
- Posts: 4486
- Joined: 23 Dec 2011 13:59
#2
Post
by Squashman » 27 Apr 2019 16:29
It would help if you told us what the error was and what line of code was producing that error.
-
penpen
- Expert
- Posts: 2009
- Joined: 23 Jun 2013 06:15
- Location: Germany
#3
Post
by penpen » 27 Apr 2019 18:05
I guess the error is something like "Maximum recursion depth exceeded." because there is a "setlocal" but no a "endlocal" in the for-loop compound statement which could be fixed by append "endlocal" as a last command to the compound statement.
penpen
-
nickodemos
- Posts: 16
- Joined: 29 Jan 2013 08:17
#4
Post
by nickodemos » 27 Apr 2019 20:03
Sorry about the late response.
I tried adding this to the end to make a hardlink but it fails with the statement below.
Code: Select all
REM move all files that start with the same base name.
mklink "!thefile!*.*" "%dir%\!thefile!\"
Code: Select all
The filename, directory name, or volume label syntax is incorrect.
-
Squashman
- Expert
- Posts: 4486
- Joined: 23 Dec 2011 13:59
#5
Post
by Squashman » 27 Apr 2019 20:37
Would have never occurred to me to think a wildcard would work with creating any type of link.
-
nickodemos
- Posts: 16
- Joined: 29 Jan 2013 08:17
#6
Post
by nickodemos » 27 Apr 2019 21:16
OK I reworked it a bit and now I can create the folder. Move the file into it. Create a symbolic link in the original directory.
Code: Select all
move "%dir%\!thefile!*.*" "%dir%\!thefile!\"
mklink "%dir%\!thefile!%1" "%dir%"
This will create the folder. Move the file into and then nothing.
Code: Select all
move "%dir%\!thefile!*.*" "%dir%\!thefile!\"
mklink /H "%dir%\!thefile!%1" "%dir%"
Problem is I need a hardlink. I understand I need admin right to run the script to make a hardlink in a batch when I try I get nothing.
-
aGerman
- Expert
- Posts: 4678
- Joined: 22 Jan 2010 18:01
- Location: Germany
#7
Post
by aGerman » 28 Apr 2019 07:42
Problem could be that the current dirctory of a batch script that was run eleveted has been automatically changed to the System32 folder. Try to include
after setlocal in your script.
Steffen
-
aGerman
- Expert
- Posts: 4678
- Joined: 22 Jan 2010 18:01
- Location: Germany
#9
Post
by aGerman » 28 Apr 2019 11:38
Consider to try with only one file first. Your construct
"%dir%\!thefile!%1"
looks weird. And
"%dir%"
sounds rather like a directory and not like a file. Hard links are not allowed for directories though.
Steffen
-
nickodemos
- Posts: 16
- Joined: 29 Jan 2013 08:17
#10
Post
by nickodemos » 28 Apr 2019 17:37
Well I am stumped at this point. What I have does work at everything but the hardlink. Guess the thread is done unless another person can come up with something else.
--Edit
Stumbled on it. Script is done.
Code: Select all
@echo off
setlocal
REM store current directory. Using separate variable makes it
REM easier to change behaviour too.
set dir=%cd%
REM call subroutine for each supported extension.
call :do .epub
call :do .mobi
call :do .pdf
call :do .jpg
REM Main program done.
echo Press a key to close.
pause
exit /b
:do
set ext=%1
REM loop through all files with the given extension.
for /f "tokens=*" %%f in ('dir /b /on "%dir%\*%ext%"') do (
REM trim the extension and use the base name as directory name.
setlocal EnableDelayedExpansion
set thefile=%%~nf
echo !thefile!
md "%dir%\!thefile!"
REM move all files that start with the same base name.
move "%dir%\!thefile!*.*" "%dir%\!thefile!\"
cd /d "%dir%\!thefile!\"
mklink /H "%dir%\!thefile!%1" "!thefile!%1"
)
REM exit subroutine
exit /b
-
aGerman
- Expert
- Posts: 4678
- Joined: 22 Jan 2010 18:01
- Location: Germany
#11
Post
by aGerman » 28 Apr 2019 18:41
Yes, now you use a file rather than a directory.
Steffen
-
pieh-ejdsch
- Posts: 240
- Joined: 04 Mar 2014 11:14
- Location: germany
#12
Post
by pieh-ejdsch » 01 May 2019 00:16
Hallo nickodemos,
does not it make sense to fold the folders with the links somewhere else than a directory deeper?
An example of how the links are used as a summary in a seperate folder you can find here?
with linked Folders
Something else
Code: Select all
@echo off
setlocal
REM store current directory. Using separate variable makes it
REM easier to change behaviour too.
REM Switch to ( another ^) directory
pushD "%cd%"
REM path Linked folder
set dirLinks=%cd%\..\Links
md "%dirLinks%"
REM call subroutine for each supported extension.
call :do .epub
call :do .mobi
call :do .pdf
call :do .jpg
REM Main program done.
popD
echo Press a key to close.
pause
exit /b
:do
REM loop through all files with the given extension.
for /f "tokens=*" %%F in ('dir /b /on "*%1"') do if exist "%%F" (
REM trim the extension and use the base name as directory name.
echo %%~dpnF
if NOT exist "%%~nF" md "%%~nF"
REM move all files that start with the same base name.
move "%%~dpnF*.*" "%%~nF\"
mklink /J "%dirLinks%\%%~nF" "%%~dpnF"
)
REM exit subroutine
exit /b