Posts: 726
Threads: 99
Joined: Mar 2018
How to use the QM code to achieve the following CMD code effect ?
I want to find a 1.txt file in all the partitions on my computer and then copy it to the desktop.
I hope someone can provide a more concise QM code, thanks in advance.
@echo off
set src=1.txt
set dst=2.txt
for %%a in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
if exist %%a:\nul (
for /f "delims=" %%i in ('dir /s/a/b/a-d "%%a:\%src%" 2^>nul') do (
copy "%%i" "%%~dpi%dst%"
)
)
)
Posts: 1,338
Threads: 61
Joined: Jul 2006
01-19-2019, 02:00 AM
(This post was last modified: 01-19-2019, 05:32 PM by Kevin.)
Try this
Function SearchDrivesForFile
str FilenameFull file drives driveletter ffn
file="1.txt"
sub.GetLogicalDrives(drives)
if(!empty(file))
,OnScreenDisplay("Searching drives for file. Please wait!" -1)
,foreach driveletter drives
,,sub.SearchForFileName(F"{driveletter}{file}" FilenameFull)
,,if FilenameFull.len
,,,foreach ffn FilenameFull
,,,,out F"{ffn} Found."
,,,,cop+ ffn "$Desktop$" FOF_ALLOWUNDO|FOF_NOCONFIRMMKDIR|FOF_NOERRORUI|FOF_FILESONLY
,,else
,,,out F"The file {file} was not Found on drive {driveletter}"
,,OsdHide
,,OnScreenDisplay("Search Complete")
#sub GetLogicalDrives
function ~&result
;dll kernel32 #GetLogicalDrives;; you may or may not need this declaration uncomment if needed
ARRAY(str) a
int i ds dm
str s(" :\") s2
dm=GetLogicalDrives ;;32-bit mask of available drives
for i 0 32
,if(dm>>i&1)
,,s[0]='A'+i
,,ds=sub.GetDriveStatus(s)
,,if ds=1
,,,a[]=s
result=a
ret
#sub SearchForFileName
function# ~file [~&result]
ARRAY(str) fl
Dir d; str s
foreach(d file FE_Dir 0|4|32)
,s = d.FileName
,if s.len
,,fl[]= d.FileName(1)
result=fl
ret
#sub GetDriveStatus
function# $drivename ;;returns: 0 does not exist, 1 ready, 2 not ready
int i=GetDriveType(drivename)
if(i=DRIVE_NO_ROOT_DIR) ret 0
int pem=SetErrorMode(SEM_FAILCRITICALERRORS)
i=GetDiskFreeSpaceEx(drivename 0 0 0)
SetErrorMode(pem)
if(i) ret 1
Posts: 726
Threads: 99
Joined: Mar 2018
01-20-2019, 12:32 AM
(This post was last modified: 01-20-2019, 12:39 AM by win.)
@kevin Thank you for your help, the above code runs successfully, but the code is long
I want to use the QM code and the BAT code to achieve the same functionality. How do I get the value of the variable %i in QM?
@echo off
for /f "tokens=2,*" %%i in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Desktop"') do (
set desk=%%j
)
set src=1.txt
for %%a in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
if exist %%a:\nul (
for /f "delims=" %%i in ('dir /s/a/b/a-d "%%a:\%src%" 2^>nul') do (
copy "%%i" "%desk%"
)
)
)
The above code, all use the BAT command, The following code, I want to use the QM+BAT command, the code that needs to supplement the variable i
@echo off
set src=1.txt
for %%a in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
if exist %%a:\nul (
for /f "delims=" %%i in ('dir /s/a/b/a-d "%%a:\%src%" 2^>nul') do (
!!!!! Some code 【i%】
)
)
)
cop %i "$Desktop$"
Posts: 1,338
Threads: 61
Joined: Jul 2006
Pfft, code is not long at all. 50 lines of code lol
check this post then
http://www.quickmacros.com/forum/showthr...6#pid29566
Posts: 726
Threads: 99
Joined: Mar 2018
01-20-2019, 01:05 AM
(This post was last modified: 01-20-2019, 01:23 AM by win.)
new code:
Macro Macro2
str s=
;set src=1.txt
;
;for %%a in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
;if exist %%a:\nul (
;;;;for /f "delims=" %%i in ('dir /s/a/b/a-d "%%a:\%src%" 2^>nul') do (
;;;;;;;;echo %%i>>c:\i.txt
;;;;)
;)
;)
__TempFile f.Init(".bat" "" "" s)
RunConsole2 f
str i.getfile("C:\i.txt")
cop i "$Desktop$"
[b]@Gintaras[/b]
[b]If the function [b][b]RunConsole2[/b] add a parameter, can get the code behind the echo, call in QM, it is much more convenient [/b][/b]
After running the bat code, or the VBS code, I need to call some variables or results in the QM code. It would be very convenient to capture the value of echo.
Posts: 1,338
Threads: 61
Joined: Jul 2006
results are already stored into str i
Posts: 726
Threads: 99
Joined: Mar 2018
01-20-2019, 01:09 PM
(This post was last modified: 01-20-2019, 01:10 PM by win.)
Macro Macro3
str d="$desktop$"
str dd=F"{d.expandpath}"
out dd
str s=
F
;set src=ttkx.txt
;
;for %%a in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
;if exist %%a:\nul (
;;;;for /f "delims=" %%i in ('dir /s/a/b/a-d "%%a:\%src%" 2^>nul') do (
;;;;;;;;copy "%%i" "{dd}"
;;;;)
;)
;)
out s
__TempFile f.Init(".bat" "" "" s)
RunConsole2 f
Posts: 1,338
Threads: 61
Joined: Jul 2006
01-20-2019, 01:51 PM
(This post was last modified: 01-20-2019, 01:52 PM by Kevin.)
you have some things not needed shortened a little bit
str d.expandpath("$desktop$")
str s=
F
;set src=ttkx.txt
;
;for %%a in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
;if exist %%a:\nul (
;;;;for /f "delims=" %%i in ('dir /s/a/b/a-d "%%a:\%src%" 2^>nul') do (
;;;;;;;;copy "%%i" "{d}"
;;;;)
;)
;)
;out s
__TempFile f.Init(".bat" "" "" s)
RunConsole2 f
also when you copy this way the file created on the desktop is overwritten so you end up with only a copy of the last file found
Posts: 726
Threads: 99
Joined: Mar 2018
01-20-2019, 08:33 PM
(This post was last modified: 01-20-2019, 08:35 PM by win.)
Thank you for your reminder, there is indeed this problem, how to solve it?
Posts: 726
Threads: 99
Joined: Mar 2018
@kevin
I have an interesting problem, my hard drive has three partitions.
First partition: C: Volume label is SYS
First partition: D: Volume label is SOFT
First partition: E: The volume label is DATA
Sometimes, I want to copy the file to the partition labeled DATA (I don't know the drive letter of the DATA partition)
So I want to get the partition drive ( E:)with the volume labeled DATA and store it in the variable s. I don't know how to use QM code. Can you help me? Thanks
Posts: 1,338
Threads: 61
Joined: Jul 2006
this should do what ya need
int i dm
str s(" :\") s2
dm=GetLogicalDrives ;;32-bit mask of available drives
for i 0 32
,if(dm>>i&1)
,,s[0]='A'+i
,,sub.GetVolumeName(s s2)
,,if s2="DATA"
,,,out s
#sub GetVolumeName
function ~DriveLetter ~&DriveName
DriveName.all(100)
GetVolumeInformation DriveLetter DriveName 100 0 0 0 0 0
DriveName.fix
if(!DriveName.len) DriveName="e" ;;not set
ret
Posts: 726
Threads: 99
Joined: Mar 2018
@kevin
Thanks for your help, the above code can find the drive letter ,
is there an easier way? such as calling the system's API, or bat commond?
I found the following code, but I don't know how to handle the character number to get the drive letter.
@echo off
for %%a in (c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) do if exist %%a: for /f "tokens=3*" %%b in ('vol %%a:') do echo %%a:Label is:%%c
pause
Posts: 1,338
Threads: 61
Joined: Jul 2006
02-13-2019, 03:10 AM
(This post was last modified: 02-13-2019, 03:12 AM by Kevin.)
finds drive by label and sets it in variable myDrive
str s=
;@echo off
;for /f %%D in ('wmic volume get DriveLetter^, Label ^| find "DATA"') do set myDrive=%%D
;echo %myDrive%\
__TempFile f.Init(".bat" "" "" s)
RunConsole2 f
Posts: 726
Threads: 99
Joined: Mar 2018
02-13-2019, 03:22 AM
(This post was last modified: 02-13-2019, 03:36 AM by win.)
Thank you very much, the above code should be the easiest way.
Does QM have Contain functions? The line where the string is located functions? I didn't find it in the help.
Macro Macro7
str s=
;hello world
;QM is very powerful
if s ???Contain "hello"
,mes "s Contains characters hello, It is in the (?) line"
Posts: 1,338
Threads: 61
Joined: Jul 2006
no to find part of a string there are numerous functions
1 example
str s ss
s=
;hello world
;QM is very powerful
int i
for i 0 2000000000
,if(ss.getl(s -i)<0) break ;;no more
,if(ss.len=0) continue ;;skip empty
,if find(ss "hello" 0 1)
,,mes F"s Contains characters hello, It is in the ({i}) line"
Posts: 726
Threads: 99
Joined: Mar 2018
It feels a bit complicated, if there is a special function, it would be fine.
Posts: 1,338
Threads: 61
Joined: Jul 2006
your asking more than 1 thing you have to search the string and then you wanted the line number.
that was 1 way
I can simplify it some but its not a built function
str s=
;hello world
;QM is very powerful
;sometext
;this is another hello
ARRAY(str) a=s
int i
for i 0 a.len
,if matchw(a[i] "*hello*" 1)
,,mes F"s Contains characters hello, It is in the ({i+1}) line"
Posts: 726
Threads: 99
Joined: Mar 2018
02-13-2019, 05:07 AM
(This post was last modified: 02-13-2019, 05:09 AM by win.)
This idea is good, thanks
Python has a lot of functions for handling strings, and it would be great if QM3 elegantly supported the introduction of PY functions.
Posts: 1,338
Threads: 61
Joined: Jul 2006
02-13-2019, 05:12 AM
(This post was last modified: 02-13-2019, 05:39 AM by Kevin.)
qm3 is c# based c# has alot of functions for strings and if needed can be used in qm2 already
Posts: 726
Threads: 99
Joined: Mar 2018
Looking forward to the release of QM3 earlier
|