Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use the QM code to achieve the following CMD code effect
#1
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. Smile
Code:
Copy      Help
@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%"
    )
 )
)
#2
Try this

Function SearchDrivesForFile
Code:
Copy      Help
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
#3
@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? Huh



Code:
Copy      Help
@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


Code:
Copy      Help
@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$"
#4
Pfft, code is not long at all. 50 lines of code lol

check this post then
http://www.quickmacros.com/forum/showthr...6#pid29566
#5
new code: Smile


Macro Macro2
Code:
Copy      Help
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]


Code:
Copy      Help
echo %%i


[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 Idea [/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. Idea
#6
results are already stored into str i
#7
Smile


Macro Macro3
Code:
Copy      Help
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
#8
you have some things not needed shortened a little bit


Code:
Copy      Help
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
#9
Thank you for your reminder, there is indeed this problem, how to solve it?  Smile
#10
@kevin

I have an interesting problem, Big Grin  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
#11
this should do what ya need


Code:
Copy      Help
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
#12
@kevin 

Thanks for your help, the above code can find the drive letter Smile

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.

Code:
Copy      Help
@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
#13
finds drive by label and sets it in variable myDrive

Code:
Copy      Help
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
#14
Thank you very much, the above code should be the easiest way. Tongue

Does QM have Contain functions? The line where the string is located functions? I didn't find it in the help.

Macro Macro7
Code:
Copy      Help
str s=
;hello world
;QM is very powerful

if s ???Contain "hello"
,mes "s Contains characters hello, It is in the (?) line"
#15
no to find part of a string there are numerous functions

1 example

Code:
Copy      Help
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"
#16
It feels a bit complicated, if there is a special function, it would be fine. Smile
#17
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



Code:
Copy      Help
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"
#18
This idea is good, thanks  Smile

Python has a lot of functions for handling strings, and it would be great if QM3 elegantly supported the introduction of PY functions.
#19
qm3 is c# based c# has alot of functions for strings and if needed can be used in qm2 already
#20
Looking forward to the release of QM3 earlier  Tongue


Forum Jump:


Users browsing this thread: 1 Guest(s)