Discussion:
Problem with file copying in Batch
(too old to reply)
mokomoji
2021-11-05 11:52:47 UTC
Permalink
z_sample.cmd - make test environment directory & file
----------------------------------------------------
@echo off
setlocal

cd /d "%~dp0"

set "z_mdpath=.\MYSLIDES\ZZZ\33-xxxiii"
set "z_rdpath=.\myslides\
rd /s /q "%z_rdpath%"
md "%z_mdpath%
echo %z_mdpath%\11.jpg>".\MYSLIDES\ZZZ\zzzz.txt"
echo %z_mdpath%\11.jpg>"%z_mdpath%\11.jpg"

endlocal
pause
------------------------------------------------------------

u'r cmd edit
------------------------------------------------------------
@echo off
setlocal
cd /d "%~dp0"

set "z_target=.\myslides\zzz"
set "z_forpath=.\myslides\zzz\zzzz.txt"

FOR /F "eol=; delims=" %%J IN (%z_forpath%) DO (
echo TRYING : Xcopy /F "%%~J" "%z_target%"
XCOPY /F "%%~J" "%z_target%\"
echo.
)

endlocal
pause

-------------------------------------------
u'r path
set "z_target=c:\myslides\zzz"
set "z_forpath=c:\myslides\zzz\zzzz.txt"
mokomoji
2021-11-05 11:58:02 UTC
Permalink
z_sample.cmd

Precautions for use
Never use it as root.
Create a separate folder and create a test environment

All folders are deleted at runtime.
mokomoji
2021-11-07 16:03:43 UTC
Permalink
Your problem is your business
up to what others have said, Am I responsible?
???????????????????????????????
mokomoji
2021-11-07 16:07:29 UTC
Permalink
i put my hands up from this unlimited responsibility.
John Stockton
2021-11-07 17:02:45 UTC
Permalink
In the meanwhile, I'll try long runs of the script only when I have another use for the hand which is not busy typing F, e.g. by eating sandwiches with it.
prompt>type FFF.TXT | ZZZ-PICK camel barnes camel
in which FFF.TXT is a readily-constructed file of thousands of lines each containing a single letter F (finishing with a line not starting with F, and not using D).
No - that runs, and shows the desired HTML; but it does not copy the files, because ZZZ\ZZZZ.TXT is empty (I don't know why)
--
(c) John Stockton, near London, UK. Using Google Groups. |
Mail: J.R.""""""""@physics.org - or as Reply-To, if any. |
LangerTom
2021-11-07 19:56:56 UTC
Permalink
In this code, the echo lines are merely for debugging
@echo FOR /F "eol=; tokens=*" %%J IN (ZZZ\ZZZZ.TXT) DO (
FOR /F "eol=; tokens=*" %%J IN (ZZZ\ZZZZ.TXT) DO (
@echo TRYING : Xcopy /F %%J ZZZ\%%J
Xcopy /F %%J ZZZ\%%J
@echo.
)
That works, except that every time Xcopy is called, I get, on-screen,
"Does C:\MYSLIDES\ZZZ\33-xxxiii\11.jpg specify a file name
or directory name on the target
(F = file, D = directory)?"
An answer of F works, but I don't want to be asked; there may be over 100 calls. I've tried XCOPY /?, but cannot recognise a suitable option.
What should I do?
If I remember correctly you should add a trailing back slash to the
target. This tells xcopy to copy files to a target directory.

xcopy /f %%J ZZZ\%%J\

(not tested)

Thomas
John Stockton
2021-11-11 18:03:07 UTC
Permalink
Post by LangerTom
That works, except that every time Xcopy is called, I get, on-screen,
"Does C:\MYSLIDES\ZZZ\33-xxxiii\11.jpg specify a file name
or directory name on the target
(F = file, D = directory)?"
An answer of F works, but I don't want to be asked; there may be over 100 calls. I've tried XCOPY /?, but cannot recognise a suitable option.
What should I do?
If I remember correctly you should add a trailing back slash to the
target. This tells xcopy to copy files to a target directory.
xcopy /f %%J ZZZ\%%J\
(not tested)
I think you have the right idea, but adjustment is needed.
A typical command line is
Xcopy /D /F /Y 37-xxxvii\20.jpg ZZZ\37-xxxvii\20.jpg
and I think I want to remove the trailing 20.jpg - but another typical line is
Xcopy /D /F /Y scans\oven\1516.png ZZZ\scans\oven\1516.png
with more sub-folders - I need to remove the characters after the LAST \ character.
The extension part will be three characters, but there could be any number of characters in the file name before the extension.

I don't know how to do that in reasonably brief pure batch.

Might %~pI be used? - seems not;
Xcopy /D /F /Y %%J ZZZ\%%~pJ copies to C:\MYSLIDES\ZZZ\MYSLIDES\37-XXXVII\20.JPG !!

I had thought, as I said, to automate typing all those F responses by piping in a file full of Fs - no good.

But it is possible to depress the F key when first asked for it, keep it down while all the XCOPY output scrolls rapidly upwards, and release it when a briskly-flowing line of Fs appears. Since this batch program will almost always be started by hand, and finishes directly after the copying, that is good enough. The repeat rate of the F key is much faster than that of my finger-pressing. If I release the F key too soon, F.BAT will maybe run (it starts File Explorer), but commands starting with multiple-F are not implemented here.



I have fixed the MiniTrue -o pipe/redirect error difficulty : I copy the input file, then in multiple MiniTrue passes on the copy, I : (0) prepend OMIT to all lines, (1 to N) alter OMIT to STET on lines wanted for various reasons, (N+1) remove the OMIT lines and the STET words. STET is logically redundant, but it helps when visually checking.

The line copy A+B+C A is not a good way to extend file A.

So : all is well enough with the image file copying, maybe.

It only remains to STET the <H#> lines and to remove, finally, all but the first of a set of consecutive such lines somehow.
And to adjust the dispositions of <ul> and/or <ol> in the source file.
--
(c) John Stockton, near London, UK. Using Google Groups. |
Mail: J.R.""""""""@physics.org - or as Reply-To, if any. |
LangerTom
2021-11-12 00:46:21 UTC
Permalink
Post by John Stockton
Post by LangerTom
That works, except that every time Xcopy is called, I get, on-screen,
"Does C:\MYSLIDES\ZZZ\33-xxxiii\11.jpg specify a file name
or directory name on the target
(F = file, D = directory)?"
An answer of F works, but I don't want to be asked; there may be over 100 calls. I've tried XCOPY /?, but cannot recognise a suitable option.
What should I do?
If I remember correctly you should add a trailing back slash to the
target. This tells xcopy to copy files to a target directory.
xcopy /f %%J ZZZ\%%J\
(not tested)
I think you have the right idea, but adjustment is needed.
A typical command line is
Xcopy /D /F /Y 37-xxxvii\20.jpg ZZZ\37-xxxvii\20.jpg
and I think I want to remove the trailing 20.jpg - but another typical line is
Xcopy /D /F /Y scans\oven\1516.png ZZZ\scans\oven\1516.png
with more sub-folders - I need to remove the characters after the LAST \ character.
The extension part will be three characters, but there could be any number of characters in the file name before the extension.
I don't know how to do that in reasonably brief pure batch.
Might %~pI be used? - seems not;
Xcopy /D /F /Y %%J ZZZ\%%~pJ copies to C:\MYSLIDES\ZZZ\MYSLIDES\37-XXXVII\20.JPG !!
I had thought, as I said, to automate typing all those F responses by piping in a file full of Fs - no good.
But it is possible to depress the F key when first asked for it, keep it down while all the XCOPY output scrolls rapidly upwards, and release it when a briskly-flowing line of Fs appears. Since this batch program will almost always be started by hand, and finishes directly after the copying, that is good enough. The repeat rate of the F key is much faster than that of my finger-pressing. If I release the F key too soon, F.BAT will maybe run (it starts File Explorer), but commands starting with multiple-F are not implemented here.
I have fixed the MiniTrue -o pipe/redirect error difficulty : I copy the input file, then in multiple MiniTrue passes on the copy, I : (0) prepend OMIT to all lines, (1 to N) alter OMIT to STET on lines wanted for various reasons, (N+1) remove the OMIT lines and the STET words. STET is logically redundant, but it helps when visually checking.
The line copy A+B+C A is not a good way to extend file A.
So : all is well enough with the image file copying, maybe.
It only remains to STET the <H#> lines and to remove, finally, all but the first of a set of consecutive such lines somehow.
And to adjust the dispositions of <ul> and/or <ol> in the source file.
I hope that I have understood you correctly. If so the following snippet
shoud show you a solution for your problem. It works with 4 sub
routines, 3 of them used internally only, for an intermediate step to
process the name of the target directory. This routines use
call-by-reference method for returning results like PASCAL.

HTH Thomas

<code>


set "J=C:\MYSLIDES\33-xxxiii\11.jpg"
call :GetPathInfo "%J%" TargetPath
echo xopy /D /F /Y %J% %TargetPath%
REM xopy /D /F /Y %%J %TargetPath%

echo %SourcePath% %TargetPath%
pause
goto :eof


REM **** sub routines *****

:GetPathInfo
call :GetSourcePath "%~1" SourcePath
call :GetSourceDirname "%sourcePath%" DirName
call :GetTargetPAth TargetPath
goto :EOF


:GetSourcePath
Set "Oldpath=%cd%"
cd /d "%~1\.."
set "%~2=%cd%"
cd /d "%oldpath%
goto :EOF


:GetSourceDirName
Set "%2=%~n1"
goto :EOF


:GetTargetPath
Set "oldpath=%cd%"
cd /d "%SourcePath%\.."
Set "%~1=%cd%\ZZZ\%Dirname%\"
cd /d "%oldPath%"
goto :EOF




</code>
John Stockton
2021-11-16 23:44:24 UTC
Permalink
Post by LangerTom
I hope that I have understood you correctly. If so the following snippet
shoud show you a solution for your problem. It works with 4 sub
routines, 3 of them used internally only, for an intermediate step to
process the name of the target directory. This routines use
call-by-reference method for returning results like PASCAL.
The need is to truncate a filename immediately
before/after its final backslash.

Thank you for your efforts. Your code uses batch
language features which I know nothing about,
and I don't want to have code which I cannot read.

A year and a half ago, I wrote code calling JScript
from Batch to do string editing using RegExps; it
is in this newsgroup. I could not get it to what I need;
I think I know why - I need a RegExp containing
parentheses within a Batch FOR loop, and as then
written that defeated the parser.

So I tried with MiniTrue, hit the same situation,
but found a satisfactory work-round. The following
code is a bit voluble. TRANSFER.BAT can be constructed
by echoing the necessary lines before the loop which uses it.


@echo Starting TRANSFER.BAT %1 JRS 2021-11-16+
@echo %1 > Z-COPIER.BAT
@MTR -n -b- Z-COPIER.BAT (.*)(\\.*) = "set ZZZZZZ=\1"
@call Z-COPIER.BAT
@del Z-COPIER.BAT
@REM echo Xcopy /D /F /Y %1 ZZZ\%ZZZZZZ%\
Xcopy /D /F /Y %1 ZZZ\%ZZZZZZ%\
@echo.
@REM The End of TRANSFER.BAT

& in the main file, with ZZZZ.TXT being the list
of image filenames :


@FOR /F "eol=; tokens=*" %%J IN (ZZZ\ZZZZ.TXT) DO @(
call TRANSFER.BAT %%J
)

which I'm almost certain can be pruned, but not tonight.

The essential is to execute the code in TRANSFER.BAT
without it being lexically in the FOR loop.

From what you wrote, I think there must be a better way
of returning the result from MTR.

As is, I think the speed is (unimportantly) limited
by screen-writing time.
--
(c) John Stockton, near London, UK. Using Google Groups. |
Mail: J.R.""""""""@physics.org - or as Reply-To, if any. |
LangerTom
2021-11-21 08:13:48 UTC
Permalink
Post by John Stockton
Post by LangerTom
I hope that I have understood you correctly. If so the following snippet
shoud show you a solution for your problem. It works with 4 sub
routines, 3 of them used internally only, for an intermediate step to
process the name of the target directory. This routines use
call-by-reference method for returning results like PASCAL.
The need is to truncate a filename immediately
before/after its final backslash.
Thank you for your efforts. Your code uses batch
language features which I know nothing about,
and I don't want to have code which I cannot read.
I found a solution for your problem, that ist very much simpler, smaller
and faster than my last suggestion.

Try:


FOR /F "eol=; tokens=*" %%J IN (ZZZ\ZZZZ.TXT) DO (
Xcopy /F %%J ZZZ\%%J\..\
)

This syntax addresses the path part only of your fully qualified
file name ZZZ\%%J and adds the trailing back space missing

.. is the representation of the parent part of a path and works on file
names in fully qualified file names too.

cd ..\.. will move upward two levels in a directory structure

HTH Thomas
John Stockton
2021-11-21 23:22:25 UTC
Permalink
Post by LangerTom
I found a solution for your problem, that ist very much simpler, smaller
and faster than my last suggestion.
FOR /F "eol=; tokens=*" %%J IN (ZZZ\ZZZZ.TXT) DO (
Xcopy /F %%J ZZZ\%%J\..\
)
This syntax addresses the path part only of your fully qualified
file name ZZZ\%%J and adds the trailing back space missing
.. is the representation of the parent part of a path and works on file
names in fully qualified file names too.
cd ..\.. will move upward two levels in a directory structure
It has taken a genius to think of something which is
perfectly obvious in hindsight! It worked immediately;
but it put too much on the console, so I now have

@FOR /F "eol=; tokens=*" %%J IN (ZZZ\ZZZZ.TXT) DO @(
@echo copy MYSLIDES\%%J to MYSLIDES\ZZZ\%%J
@Xcopy %%J ZZZ\%%J\..\ > nul
)

which gives just the echoed line for each file copied.
It does give more if the apparent source file does not
exist; but that will not happen when I have completed
a recalcitrant piece of the preceding coding.

Thanks,
--
(c) John Stockton, near London, UK. Using Google Groups. |
Mail: J.R.""""""""@physics.org - or as Reply-To, if any. |
LangerTom
2021-11-22 21:47:45 UTC
Permalink
You are welcome.
Post by John Stockton
Post by LangerTom
I found a solution for your problem, that ist very much simpler, smaller
and faster than my last suggestion.
FOR /F "eol=; tokens=*" %%J IN (ZZZ\ZZZZ.TXT) DO (
Xcopy /F %%J ZZZ\%%J\..\
)
This syntax addresses the path part only of your fully qualified
file name ZZZ\%%J and adds the trailing back space missing
.. is the representation of the parent part of a path and works on file
names in fully qualified file names too.
cd ..\.. will move upward two levels in a directory structure
It has taken a genius to think of something which is
perfectly obvious in hindsight! It worked immediately;
but it put too much on the console, so I now have
@FOR /F "eol=; tokens=*" %%J IN (ZZZ\ZZZZ.TXT) DO @(
@echo copy MYSLIDES\%%J to MYSLIDES\ZZZ\%%J
@Xcopy %%J ZZZ\%%J\..\ > nul
)
which gives just the echoed line for each file copied.
It does give more if the apparent source file does not
exist; but that will not happen when I have completed
a recalcitrant piece of the preceding coding.
Thanks,
mokomoji
2021-11-12 18:45:59 UTC
Permalink
z_sample.cmd
---------------------------------------------------------------------------------
@echo off

setlocal
cd /d "%~dp0"
echo "%~d0\"|find /i "%cd%" 2>nul>nul&&(
echo now folder root in not run~!
goto :end
)

set "z_mdpath.1=.\MYSLIDES\ZZZ\33-xxxiii"
set "z_mdpath.2=.\MYSLIDES\ZZZ\scans\oven"
set "z_rdpath=.\myslides\

rd /s /q "%z_rdpath%"
md "%z_mdpath.1%
md "%z_mdpath.2%

echo %z_mdpath.1%\11.jpg>".\MYSLIDES\ZZZ\zzzz.txt"
echo %z_mdpath.1%\20.jpg>>".\MYSLIDES\ZZZ\zzzz.txt"
echo %z_mdpath.2%\1516.jpg>>".\MYSLIDES\ZZZ\zzzz.txt"
echo %z_mdpath.1%\11.jpg>"%z_mdpath.1%\11.jpg"
echo %z_mdpath.1%\20.jpg>"%z_mdpath.1%\20.jpg"
echo %z_mdpath.2%\1516.jpg>"%z_mdpath.2%\1516.jpg"
:end
endlocal
pause
exit
----------------------------------------------------------------------------------------------

run.cmd
--------------------------------------------------------------------------------------
@echo off
setlocal
cd /d "%~dp0"

set "z_target=.\myslides\zzz"
set "z_forpath=%z_target%\zzzz.txt"
for /f "eol=; delims=" %%j in (%z_forpath%) do (

echo TRYING : xcopy /f "%%~j" "%%~dpj\%%~pj\"
xcopy /f "%%~j" "%z_target%\%%~pj\"
echo.
)
endlocal
pause
------------------------------------------------------------------------------------------------
godthem...
Loading...