short way to input with xcopy

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pieh-ejdsch
Posts: 240
Joined: 04 Mar 2014 11:14
Location: germany

short way to input with xcopy

#1 Post by pieh-ejdsch » 14 Apr 2014 13:26

Hallo,

I've seen this in most batches:

Code: Select all

for /f "delims=" %%A in ('xcopy /w "%~f0" "%~f0" 2^>nul') do (
      if not defined key set "key=%%A"
    )
    set "key=!key:~-1!"



There is a simpe way to read the first line:
The output with file is not promt when use /Q
In destination I use "NUL:\*" - this isn't a Drive or a Location but no output for error. And the message with /L Option is anyone OK.

Code: Select all

for /F "eol=1delims=" %%x in ('xcopy /WQL "%~f0" NUL:\*') do (set "key=%%x"
  set "key=!key:~-1!"
)


To read as any more input as once use an Intitialize-Token from xcopy output like this

Code: Select all

@echo off
setlocal disabledelayedexpansion
:init_token_Keypress
for /f %%n in ('echo( ^|cmd /q /u /c "for /f "eol^=1delims^=" %%E in ('xcopy /WQL "%comspec%" nul:\*') do echo(%%E"^|find /c " "') do set /a Keytoken= %%n

:Input
(
for /F "eol=1tokens=%keytoken%delims= " %%x in ('xcopy /WQL "%~f0" NUL:\*') do set "key=%%x"
)|| set "key= "
goto :Input


Phil

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: short way to input with xcopy

#2 Post by aGerman » 14 Apr 2014 17:00

Good idea. It's more consistent to avoid errors instead of ignoring them. Also the EOL=1 is a nice workaround to process only the first line. I hope it's language independent (means that the number of copied files must be the biginning of the line).

Regards
aGerman

einstein1969
Expert
Posts: 960
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: short way to input with xcopy

#3 Post by einstein1969 » 15 Apr 2014 03:35

I agree. It's a good idea. I have tested on my win 7 32 italian and the only problem is for the second trick.

My xcopy don't put a space after the prompt for keypress.

Code: Select all

>xcopy /WQL $$$$ NUL:\*
Premere un tasto per cominciare a copiare dei file


after press A:

Code: Select all

>xcopy /WQL $$$$ NUL:\*
Premere un tasto per cominciare a copiare dei fileA
1 File


then need the set "key=!key:~-1!"

The first trick work well. Thanks Phil.

einstein1969

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: short way to input with xcopy

#4 Post by foxidrive » 15 Apr 2014 03:59

There is a simpe way to read the first line:
The output with file is not promt when use /Q
In destination I use "NUL:\*" - this isn't a Drive or a Location but no output for error. And the message with /L Option is anyone OK.

Code: Select all

for /F "eol=1delims=" %%x in ('xcopy /WQL "%~f0" NUL:\*') do (set "key=%%x"
  set "key=!key:~-1!"
)



It does seem useful.

To read as any more input as once use an Intitialize-Token from xcopy output like this

Code: Select all

@echo off
setlocal disabledelayedexpansion
:init_token_Keypress
for /f %%n in ('echo( ^|cmd /q /u /c "for /f "eol^=1delims^=" %%E in ('xcopy /WQL "%comspec%" nul:\*') do echo(%%E"^|find /c " "') do set /a Keytoken= %%n

:Input
(
for /F "eol=1tokens=%keytoken%delims= " %%x in ('xcopy /WQL "%~f0" NUL:\*') do set "key=%%x"
)|| set "key= "
goto :Input



How is this meant to work, Phil?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: short way to input with xcopy

#5 Post by aGerman » 15 Apr 2014 13:16

foxidrive wrote:How is this meant to work, Phil?

:init_token_Keypress counts the tokens in the first line. As einstein1969 already mentioned it only works if the pressed key is separated with a space like in my output (the space next to "starten" is already part of the XCOPY output).

Code: Select all

xcopy /WQL "%~f0" NUL:\*

Eine beliebige Taste drücken, um das Kopieren der Datei(en) zu starten x
1 Datei(en) kopiert

Of course you have to include some meaningful code into the :Input loop :wink:

Some changed examples:
Hidden_Input.bat

Code: Select all

@echo off &setlocal

<nul set /p "=Enter your password: "
call :HInput pw
echo Input length is %errorlevel%
setlocal EnableDelayedExpansion
echo Your password is !pw!
pause
goto :eof


:HInput [ByRef_VarName]
:: inspired by Carlos
:: improved by pieh-ejdsch
if "%__HI__%" neq "__HI__" (
  setlocal DisableDelayedExpansion
  set "S=" &set "N=0" &set "__HI__=__HI__"
  for /f %%i in ('"prompt;$h&for %%i in (1) do rem"') do set "BS=%%i"
)
set "C="
for /f "eol=1 delims=" %%i in ('xcopy /lwq "%~f0" :\') do set "C=%%i"
set "C=%C:~-1%"
setlocal EnableDelayedExpansion
if not defined C (
  echo(
  if "%~1"=="" (
    echo(!S!
    endlocal &endlocal &exit /b %N%
  ) else (
    if defined S (
      for /f delims^=^ eol^= %%i in ("!S!") do endlocal &endlocal &set "%~1=%%i" &exit /b %N%
    ) else endlocal &endlocal &set "%~1=" &exit /b 0
  )
)
if "!BS!"=="!C!" (
  set "C="
  if defined S set /a "N -= 1" &set "S=!S:~,-1!" &<nul set /p "=%BS% %BS%"
) else set /a "N += 1" &<nul set /p "=*"
if not defined S (
  endlocal &set "N=%N%" &set "S=%C%"
) else for /f delims^=^ eol^= %%i in ("!S!") do endlocal &set "N=%N%" &set "S=%%i%C%"
goto HInput


Choice_Imitation.bat

Code: Select all

@echo off &setlocal
<nul set /p "=Yes or No [yn]: "
call :choice yn
echo Option #%errorlevel% was chosen.
pause

goto :eof


:choice ByVal_Choices
setlocal DisableDelayedExpansion
set "n=0" &set "c=" &set "e=" &set "map=%~1"
if not defined map endlocal &exit /b 0
for /f "eol=1 delims=" %%i in ('xcopy /lwq "%~f0" :\') do set "c=%%i"
set "c=%c:~-1%"
if defined c (
  for /f delims^=^ eol^= %%i in ('cmd /von /u /c "echo(!map!"^|find /v ""^|findstr .') do (
    set /a "n += 1" &set "e=%%i"
    setlocal EnableDelayedExpansion
    if /i "!e!"=="!c!" (
      echo(!c!
      for /f %%j in ("!n!") do endlocal &endlocal &exit /b %%j
    )
    endlocal
  )
)
endlocal &<nul set /p "=" &goto choice

I left the %var:~-1% technique since we know counting the tokens could fail.

Regards
aGerman

pieh-ejdsch
Posts: 240
Joined: 04 Mar 2014 11:14
Location: germany

Re: short way to input with xcopy

#6 Post by pieh-ejdsch » 15 Apr 2014 15:10

@aGerman thank you for that explanation. I can't do that - my bad english :oops:

So I have competed the code, that the last Token before Input is Autoset.
At other language settings for example.

Code: Select all

@echo off
setlocal disabledelayedexpansion

:init_token_Keypress
for /f "eol=1delims=#" %%d in ('"echo(#|xcopy /WQL %comspec% nul:\*"') do set "Line=%%d"
set "delim=%line:~-1%"
for /f %%n in ('^(echo^(^&echo^("%%Line:%delim%="^&echo^("%%"^)^|find /c /v """"""') do set /a token= %%n

:init_Vars
set Enter=3
for /f %%+ in ('copy /Z "%~dpf0" nul') do (rem CR
 for /f %%- in ('"prompt;$h&for %%i in (1) do rem"') do (set "bs=%%-" &rem BackSpace
  echo Input ....
  call :Input %enter%
))
exit /b

:Input
for %%. in (.) do (
 for /F eol^=1tokens^=%token%delims^=^%delim% %%x in ('xcopy /WQL "%~f0" NUL:\*') do (
  if %%x equ %%+ if ^%enter%  equ 1 exit /b
  if %%x equ %%- (
   set key=%%x %%x%%x %%x%%x %%x
  ) else set "key=%%x"
 )
) || set "key=%delim%"

<nul set/p=*^%key%
:>nul&set /a enter-=1 &echo(&goto :Input
set /a enter=%1
goto :Input


And a bug with <CR> I found. argh not. It means the CR is not Present at %var% expansion.
Behind Output is a command beginning with :
When Variable Key is <CR> btw empty - the line will end in Next and proceed this.

Phil

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: short way to input with xcopy

#7 Post by foxidrive » 15 Apr 2014 16:54

aGerman wrote:
foxidrive wrote:How is this meant to work, Phil?

:init_token_Keypress counts the tokens in the first line. As einstein1969 already mentioned it only works if the pressed key is separated with a space like in my output (the space next to "starten" is already part of the XCOPY output).

Code: Select all

xcopy /WQL "%~f0" NUL:\*

Eine beliebige Taste drücken, um das Kopieren der Datei(en) zu starten x
1 Datei(en) kopiert

Of course you have to include some meaningful code into the :Input loop :wink:


Thanks aGerman.

einstein1969
Expert
Posts: 960
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: short way to input with xcopy

#8 Post by einstein1969 » 16 Apr 2014 19:14

Hi,

This approch use few cpu on multicore but on my monocore is high!!

Can I reduce the cpu usage using:

Code: Select all

mode con: rate=12


but why not use this with multithreading batch (START /B) or pipe?

Code: Select all

xcopy /Lp "%~f0" NUL:\*


einstein1969

pieh-ejdsch
Posts: 240
Joined: 04 Mar 2014 11:14
Location: germany

Re: short way to input with xcopy

#9 Post by pieh-ejdsch » 17 Apr 2014 11:42

In your code, for execution is not terminated if not j; n or y n, etc. depending on the language setting is pressed. At a keystroke, the researchers bight could be completed in a subroutine through an exit / b. However, if the option is only an indefinite keystroke to read something, then I use this variant. How have you implemented this in your code?
In the output of the second line is on success (enter yes ) a 1 at the beginning, made ​​with nothing ( no input ) a 0 at the beginning. This would be reissued with FIND "/" only the first line. My goal was to limit the output of the researchers bight without a second command to an output.

Phil

EDIT by aGerman: translated the German text that Phil gave me via PM

In your code the FOR loop will not be terminated until you pressed j,n or y,n (depending on your local settings). You could terminate a subroutine with EXIT /B by pressing a key though. However, if the goal is to read any keystroke I use the technique I posted above. How did you implement it?

The second line of the output shows a 1 at the beginning of the line if you confirmed or a 0 if you denied the request for copying. You would have to use FIND "/" to get only the first line. My goal was to get the result directly out of the FOR loop without using an additional command.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: short way to input with xcopy

#10 Post by Ed Dyreen » 17 Apr 2014 22:15

pieh-ejdsch wrote:In destination I use "NUL:\*" - this isn't a Drive or a Location but no output for error. And the message with /L Option is anyone OK.
And since relative paths are supported "NUL:\*" <==> "\"

Code: Select all

@echo off &prompt $g &setlocal enableDelayedExpansion

set "$=" &for /f "eol=1 delims=" %%? in ( 'xcopy.EXE /WQL "%comspec%" \' ) do set "$=%%?" &set "$=!$:~-1!"
echo.$=!$!_
pause
exit
I also found ":\" to be accepted, the "it's a valid path check" shows to be minimalistic and buggy.

einstein1969
Expert
Posts: 960
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: short way to input with xcopy

#11 Post by einstein1969 » 20 Apr 2014 15:54

pieh-ejdsch wrote:In your code, for execution is not terminated if not j; n or y n, etc. depending on the language setting is pressed. At a keystroke, the researchers bight could be completed in a subroutine through an exit / b. However, if the option is only an indefinite keystroke to read something, then I use this variant. How have you implemented this in your code?
In the output of the second line is on success (enter yes ) a 1 at the beginning, made ​​with nothing ( no input ) a 0 at the beginning. This would be reissued with FIND "/" only the first line. My goal was to limit the output of the researchers bight without a second command to an output.

Phil


Hi Phil,

I have not complete understaind what you write in your post . My goal seem different.
I need to manage multiple input at high rate. I don't care if xcopy finish with "s" or "j". I reRun for continuos input.

I have probed this but i have difficult to stabilize. This can manage 10 keystroke at time. I have reRun the xcopy for 3 time because i have not implemented a method for exit.

this is a test at command prompt

Code: Select all

for /l %n in (1,1,3) do @xcopy /pl $$$$ $$$$ 2>nul | cmd /q /v:o
n /c"echo Start & For /L %# in (1,1,10) do @(echo %#&set L=&set /p L=&if defined
 L (echo(%#:'!L!'|find /V "$$$$") else (echo %#:!L! exit ) ) "


EDIT: $$$$ is a temporary file that must exist

einstein1969

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: short way to input with xcopy

#12 Post by aGerman » 20 Apr 2014 16:36

[OT]
I have not complete understaind what you write in your post .

Phil was so kind and wrote a PM with the origin German text. I appended my translation to his post above (even if my English is anything but perfect).

@Phil I hope you agree with my translation. Please complain otherwise.

Regards
aGerman

[/OT]

pieh-ejdsch
Posts: 240
Joined: 04 Mar 2014 11:14
Location: germany

Re: short way to input with xcopy

#13 Post by pieh-ejdsch » 18 Aug 2014 12:33

einstein1969 wrote:Hi Phil,

I have not complete understaind what you write in your post . My goal seem different.
I need to manage multiple input at high rate. I don't care if xcopy finish with "s" or "j". I reRun for continuos input.

I have probed this but i have difficult to stabilize. This can manage 10 keystroke at time. I have reRun the xcopy for 3 time because i have not implemented a method for exit.

this is a test at command prompt

Code: Select all

for /l %n in (1,1,3) do @xcopy /pl $$$$ $$$$ 2>nul | cmd /q /v:on /c"echo Start & For /L %# in (1,1,10) do @(echo %#&set L=&set /p L=&if defined L (echo(%#:'!L!'|find /V "$$$$") else (echo %#:!L! exit ) ) "


EDIT: $$$$ is a temporary file that must exist

einstein1969


Your code for cmd prompt ends:

temporary files not needed,
supports strg+C

Code: Select all

for /f "tokens=1-7delims=-" %O in ("xcopy/lp %comspec% nul:\*-Abort-exit /b-set x=-set/p-defined-echo(") do @for /f "tokens=2,3delims=(/)" %M in ('"%O<nul|(%S:=&set:)"') do @(%O||<nul %S"=%P")|(cls&%Uinput Ends with [STRG]+[C] or [%M] or [%N]&cmd/v/k"%R1&for /L %L in (.) do @set .=&%S.=&(if !.! equ %P %U%P!&%Q)&if not %T . (if %T x (%U- your key is DEL, TAB or Enter&%R) else %R1) else if !.:~1^,1!. equ . %U!.!&%R& if /i !.! equ %M (%Q) else if /i !.! equ %N %Q")


How/where did you implement this code into Batch?

Phil

Post Reply