

InterviewSolution
1. |
Solve : C++ trying to figure out how to clip file extensions for project? |
Answer» I have a project I am working on in C++ using VC++ 6 and I am reading in a text file that is output from a batch that runs a series of dir/b *.extension>>list.txt routines to append to file all file extensions I want to have in my list file instead of just all files in that directory. This is used to list all the movie/VIDEOS to this text file. The C++ is used to create/compile a HTML page for my HTPC. echo. <HTML><BODY>>>htpc.htmalthough I am not sure if escape characters would be needed to not confuse the batch with the < and > tags of the html, and the planting of the information in a looped process that performs line creation until eof of something similar to Quote echo. <a href"(variable [A] here to pass full filename and extension)">(variable here to pass the full file name without extension) [/url]>>htpc.htm/a instead of /url above within < > instead of [ ] ... this is being translated in this page LOL at using a sledgehammer to crack a nut... yah I guess that is a good analogy to what I was doing! I suppose I was using a sledgehammer because it will squash the nut, while I was unsure if batch was going to be like hitting the nut with a butter knife..lol ... Not to bash batches ability to get the job done, but I guess just using an overkill tool to get the job done because I was sure C++ would do it and unsure as to if batch could do it on its own without running into a limitation. I came up with this but it's useless and doesn't work for some reason. Code: [Select]REM output HTML file: setolocal enabledelayedexpansion SET outhtml=D:\htpc.html SET searchpath=D:\music\ echo ^<html^>^<body^>^<center^> > %outhtml% for /f "delims=*" %%P in ('dir /b %searchpath%*.mp3') do ( echo "%%P" call :separate %%P echo ^<A href="!searchpath!%%P"^>%FILE%^</a^> >> %outhtml% ) echo ^</center^>^</body^>^</html^> >>%outhtml% goto EOF :separate set FILE=%~n1 set EXT=%~x1 exit /b 0 :EOF You should stick with whatever you feel comfortable with... I prefer batch or other scripting languages (especially Powershell which (I don't personally think) has such a steep learning curve as some people make out) because you can run the thing and see if it works without having to compile it first. A thought: I expect you "normalise" your file names first so they don't look like this "Nightmare.on.Elm.Street.[XVID].REPACK.Superfly.mp4" where I guess you would isolate the extension by counting backwards from the end of the string until you hit a dot. (of course you wouldn't have files like that would you???!!! Because we're talking about fair-use format shifting of stuff we own. Of course. Anyhow Windows batch has "variable modifiers" (see the FOR help accessible via FOR /? for details) to help you get at file properties. If %%A is a file name then %%~dA is the drive letter (with colon) and %%~pA is the path (folder), %%~nA is the name part and %%~xA is the extension (with dot) so if you did this you would echo just the file names FOR /F "delims=" %%A in ('dir /b *.mp4') do ( echo %%~nA ) You can echo characters like < and > if you escape them with carets like this ^< so you could make a simple html file like this FOR /F "delims=" %%A in ('dir /b *.mp4') do ( echo %%~nA^ >> mymenu.html ) Code: [Select]S:\Test\movie list>dir Volume in drive S is USB-1 Volume Serial Number is 2C51-AA7F Directory of S:\Test\movie list 09/08/2011 21:16 <DIR> . 09/08/2011 21:16 <DIR> .. 06/06/2011 18:56 185,665,722 1307293768080.mp4 07/06/2011 18:36 168,581,204 1307401963926.mp4 14/06/2011 17:41 174,549,670 1308008220765.mp4 22/06/2011 20:40 401,292,105 1308705903296.mp4 09/08/2011 21:16 289 make-html.bat 17/05/2011 00:49 172,288,053 Mapa sonoro Antonia Font 16-05-11.mp4 09/08/2011 21:16 1,036 mymenu.html 07/05/2011 21:08 331,807,998 Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_10_b010w7m9_default.mp4 03/04/2011 09:55 313,634,325 Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_1_b01075w0_default.mp4 03/04/2011 10:00 332,694,729 Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_2_b01083xt_default.mp4 10/04/2011 10:32 321,423,288 Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_3_b0109bll_default.mp4 10/04/2011 10:41 329,109,128 Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_4_b0109bln_default.mp4 17/04/2011 15:46 334,111,133 Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_5_b010j4ms_default.mp4 17/04/2011 15:51 348,788,609 Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_6_b010j4mv_default.mp4 30/04/2011 21:23 327,717,522 Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_7_b010p4x2_default.mp4 30/04/2011 21:27 323,716,560 Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_8_b010p4x4_default.mp4 07/05/2011 21:05 314,839,606 Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_9_b010w7m7_default.mp4 17/05/2011 00:49 172,288,053 test.mp4 18 File(s) 4,552,509,030 bytes 2 Dir(s) 237,718,470,656 bytes free Code: [Select]echo off echo ^<html^> > mymenu.html echo ^<body style="font-family:Arial; font-size:18px;margin-left:50px"^> >> mymenu.html echo Movie List^</br^> >> mymenu.html FOR /F "delims=" %%A in ('dir /b *.mp4') do ( echo %%~nA^</br^> >> mymenu.html ) echo ^</html^> >> mymenu.html Code: [Select]S:\Test\movie list>type mymenu.html <html> <body style="font-family:Arial; font-size:18px;margin-left:50px"> Movie List</br> 1307293768080</br> 1307401963926</br> 1308008220765</br> 1308705903296</br> Mapa sonoro Antonia Font 16-05-11</br> Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_10_b010w7m9_default</br> Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_1_b01075w0_default</br> Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_2_b01083xt_default</br> Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_3_b0109bll_default</br> Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_4_b0109bln_default</br> Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_5_b010j4ms_default</br> Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_6_b010j4mv_default</br> Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_7_b010p4x2_default</br> Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_8_b010p4x4_default</br> Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_9_b010w7m7_default</br> test</br> </html> Quote from: BC_Programmer on August 09, 2011, 01:52:18 PM I came up with this but it's useless and doesn't work for some reason. Code: [Select]'setolocal' is not recognized as an internal or external command, operable program or batch file. No comment... Code: [Select]The syntax of the command is incorrect. S:\Test\movie list>for /f "delims=*" %P in ('dir /b S:\Test\movie list*.mp4') do You want the open parenthesis on the same line as the DO Code: [Select]REM output HTML file: REM not needed! setlocal enabledelayedexpansion SET outhtml=D:\htpc.html REM I changed this to suit my test folder SET searchpath=%cd% echo ^<html^>^<body^>^<center^> > %outhtml% REM OK now I ut a backslash in for /f "delims=*" %%P in ('dir /b "%searchpath%\*.mp4"') do ( echo "%%P" REM Colon is not obligatory REM You can do this inline anyhow call separate %%P echo ^<A href="%searchpath%%%P"^>%FILE%^</a^> >> %outhtml% ) echo ^</center^>^</body^>^</html^> >>%outhtml% REM Gotta skip over the sub somehow goto end :separate set FILE=%~n1 set EXT=%~x1 REM This is how you get out of the sub REM There isn't actually a label CALLED :EOF REM This MEANS RETURN REM This colon is obligatory goto :EOF :end REM We're done Code: [Select]<html><body><center> <A href="S:\Test\movie list1307293768080.mp4"></a> <A href="S:\Test\movie list1307401963926.mp4"></a> <A href="S:\Test\movie list1308008220765.mp4"></a> <A href="S:\Test\movie list1308705903296.mp4"></a> <A href="S:\Test\movie listMapa sonoro Antonia Font 16-05-11.mp4"></a> <A href="S:\Test\movie listSpiral_Series_3_-_The_Butcher_of_La_Villette_Episode_10_b010w7m9_default.mp4"></a> <A href="S:\Test\movie listSpiral_Series_3_-_The_Butcher_of_La_Villette_Episode_1_b01075w0_default.mp4"></a> <A href="S:\Test\movie listSpiral_Series_3_-_The_Butcher_of_La_Villette_Episode_2_b01083xt_default.mp4"></a> <A href="S:\Test\movie listSpiral_Series_3_-_The_Butcher_of_La_Villette_Episode_3_b0109bll_default.mp4"></a> <A href="S:\Test\movie listSpiral_Series_3_-_The_Butcher_of_La_Villette_Episode_4_b0109bln_default.mp4"></a> <A href="S:\Test\movie listSpiral_Series_3_-_The_Butcher_of_La_Villette_Episode_5_b010j4ms_default.mp4"></a> <A href="S:\Test\movie listSpiral_Series_3_-_The_Butcher_of_La_Villette_Episode_6_b010j4mv_default.mp4"></a> <A href="S:\Test\movie listSpiral_Series_3_-_The_Butcher_of_La_Villette_Episode_7_b010p4x2_default.mp4"></a> <A href="S:\Test\movie listSpiral_Series_3_-_The_Butcher_of_La_Villette_Episode_8_b010p4x4_default.mp4"></a> <A href="S:\Test\movie listSpiral_Series_3_-_The_Butcher_of_La_Villette_Episode_9_b010w7m7_default.mp4"></a> <A href="S:\Test\movie listtest.mp4"></a> </center></body></html> but it doesn't work in my browser... Thanks Salmon for showing me how to do that in batch. I have gone in and changed my file names manually for many of the MP4's since some were created as DVD_unknown.mp4 for example and then I have to go in and type Cheech_And_Chong__Things_Are_Tough_All_ Over.mp4 and switch like Terminator4x3.mp4 to just Terminator.mp4. I am using Format Factory to generate many of my MP4's off of my DVD collection and it worked really well for about 95% of my movies. Other DVD's that don't like it I can use my capture card and software in the HTPC to act like a DVR to play to the HTPC from my DVD player and create the MP4 the long way for my home theater setup. The DVD's and VHS tapes then go into storage and I have my entire movie collection on a 500GB drive which is currently about 200GB. My wife the one day said oh, well since you converted those movies we can now get rid of them and sell them... I SAID NOOOO WAY!!! Its Piracy if we dont retain ownership of the movies! So we legally have to keep physical posession of them for as long as we own the digital copy of it. She agreed then to let the box of movies stay in the closet untampered with..lol So I am only creating "Fair-Use" Format Shifted COPIES! Most movies are about 700MB to 1.2GB in size created as 720 format. Since I only have a 32" screen 720 is fine. Since I want to know how to do it in both Batch & C++ now, and have some compile errors in C++, I am going to continue this thread with the errors I got with BC's code to figure out why I am having compile issues. It might be as simple as my compiler is different than BC's. I ran into a problem years ago when i was writing C++ in both Borland 4.0 that I owned which came bundled with a Borland book, and MS VC++ 5.0 that the college owned at the time in which one liked the header or handler I think its called #include and the other preferred #include...so i tried to remove the .h from #include since the rest were without .h and got a single error of windows not found or something along those lines. Also went and added .h to those without.h to make all the same statement style and it then gave me like 13 errors instead of 4 compiling. So below is the error I get that maybe you can point me in the right direction to resolve since it doesnt look very straight forward to me as a error of missing a ; etc would be. I have never used the Windows.h header/handler before so maybe I need to do something other than just copy/paste and compile. In the past with other programs calling out to custom .h files, I remember having to go in and build .h files to be present for the compilation when it links it to the main program. I have never written my own .h file to link to and mainly have coded within the console programming C/C++ environment as taught in college. I kind of feel like I was passed through the system when it comes to C++ programming as for in advanced C++ we were only dealing with CLASSES. Leaving college with a Computer Systems Management (MIS) Degree and favoring C++ over other languages and digging into the more advanced stuff, i feel as if they should have covered classes when I took the 2nd course Intermediate C++, then really dove into the advanced stuff in the advanced C++ the 3rd semester to go into stuff like what BC has coded here that is new territory and I am soaking up as much info as I can from it. The other thing I need to do is stay active with it too instead of just coding when I need something, since if you dont use it you lose it. Similar to how I passed advanced math with calculus and loved trig, and when going to assist a friend with his math the one day after not doing it for almost 10 years, I knew some stuff right off, but other stuff I was like hmm...lets look it up..lol If I played with it after learning it, I would have retained it better and programming is the same way I feel. Quote --------------------Configuration: htpc3 - Win32 Debug--------------------Compiles in VC++ 2008. Your problem (assuming it isn't also related to compiler versions) is that your defs are set to use ANSI rather than Unicode. I think that is the default for earlier versions of the MS compiler. This breaks the assumption that fdata->cFileName is a WCHAR_T, as well as others. enter #define UNICODE before the windows.h include. Quote The other thing I need to do is stay active with it too instead of just coding when I need something, since if you dont use it you lose it.I hardly ever write C++ code, personally. I know what you mean but I don't think it applies as strongly as you think. There is certainly no reason to shoehorn projects into a C++ mould.Thanks BC I'll give that a try on my VC++ 6.0... I figured it was compiler version related. Never knew about the ANSI vs Unicode change. I have been tempted to install a newer version of MS C++ such as 2008 Express which came bundled with my VC++ 2008 Book, but the clunker workhorse laptop I use while on breaks at work is a Pentium III 600Mhz with 384MB Ram running clean unpatched oem install of XP Pro SP2, and patching it to SP3 and all updates for a laptop that remains as offline usage brings it to a horrible crawl, so I figured going through installing 2008 and Dot NET etc might be too much for it to handle. I have VC++ 2008, VB 2008, and C# 2008 installed on my desktop home computer and played with them some. Only complaint I have with them is that whatever you write you need to make sure that Dot NET 3.5 I think the version is is installed on any system that you execute the compiled programs from. Its too bad they cant squeeze that into the .EXE so it can be stand alone I guess you could call it vs requiring the 3.5 framework that most systems I have seen dont normally update to with MS updates. I think the MS updates bring systems to Dot NET 2.0. Salmon... I tried your batch and tweaked it some to work on my system which is executing it on C: instead of D: and the problem I am seeing same as what your output shows is that the instruction to pass %FILE% into the created HTML line that is written to HTPC.htm seems like its not getting populated with the File Name information. For some reason, %FILE% in the line below isnt inherriting the file name from set FILE=%~n1 so because of this the page loads as an empty page with no text linked when you load the htpc.htm page. Code: [Select]echo ^<A href="%searchpath%%%P"^>%FILE%^</a^> >> %outhtml%Here is what is created from the batch when changing SET outhtml=D:\htpc.html to SET outhtml=C:\htpc.html since my D: drive is my DVD drive. I also have to change SET searchpath=%cd% to SET searchpath=%cc% , which was a guess on my part that the "d" meant D: drive and so the last "c" would be C: drive, and that gives me the output of Code: [Select]<html><body><center> <A href="test1.mp4"></a> <A href="test2.mp4"></a> <A href="test3.mp4"></a> </center></body></html> Instead of Code: [Select]<html><body><center> </center></body></html> so I end up with what you get...so still stuck at %FILE% not POPULATING with the File Name without the extension portion. Once this file name issue is fixed for populating %FILE% we should end up with Code: [Select]<html><body><center> <A href="test1.mp4">test1</a> <A href="test2.mp4">test2</a> <A href="test3.mp4">test3</a> </center></body></html>the batch you used there was the one I originally created, with his modifications. As he noted, it doesn't work. I don't think it is worth fixing since he already offered a batch solution. Quote from: DaveLembke on August 09, 2011, 06:52:28 PM Only complaint I have with them is that whatever you write you need to make sure that Dot NET 3.5 I think the version is is installed on any system that you execute the compiled programs from. Its too bad they cant squeeze that into the .EXE so it can be stand alone I guess you could call it vs requiring the 3.5 framework that most systems I have seen dont normally update to with MS updates. I think the MS updates bring systems to Dot NET 2.0.And yet, nobody seems to consider the necessity of a JVM and the Java class libraries being installed to run java programs as a point against Java development. Almost every Development tool will require something of the host machine in the way of libraries for any non-trivial project. All Versions of Visual Studio .NET allow you to target a different framework version. You can create programs that target .NET 2.0 in Visual Studio 2008. I don't, because there is no point. If somebody doesn't have .NET 3.5 installed, they can install it. I'm not about to do more work getting things working with .NET 2.0 just so somebody can be lazy; additionally, Windows Vista and 7 come with .NET 3.5 anyway, and I don't make a habit of targeting 10 year old Operating Systems. Also of interest: if you do it right, you can create executables that run on both Windows, OSX, and Linux using .NET. Which is an advantage. Quote from: DaveLembke on August 09, 2011, 06:52:28 PM I also have to change SET searchpath=%cd% to SET searchpath=%cc% , which was a guess on my part %cd% is a special Windows-supplied variable - it holds the drive letter and path of the current directory at the time the code is run. %cc% will presumably just be blank. Anyhow as BC_P said, I was just playing around with his code. You could try the code in my post, the one which starts "You should stick with whatever you feel comfortable with..." but on that note, it may be that going into batch was a step in the wrong direction, and I apologise for hijacking the thread to no good purpose. Quote from: DaveLembke on August 08, 2011, 08:29:30 PM So I figured, I'd run a quick batch, then have my list.txt file ready for me with the files I want to have included in the created HTPC webpage and then use the C++ to then read in this text file and run with it. You could just modify the batch you run first. Instead of this... dir /b *.mp4 >> list.txt ...you could use this, which does the same thing except it strips off the .mp4 extension: for /f "delims=" %%A in ('dir /b *.mp4') do echo %%~nA >> list.txt |
|